Superclass variable reference with subclass object

本文探讨了面向对象编程中继承的概念,特别是超类引用变量可以指向任何从该超类派生的子类实例这一特性。文章强调了这种设计灵活性的重要性和应用场景,并指出访问子类对象的成员时,实际上是通过超类引用变量的类型来决定可以访问哪些成员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A reference variable of a superclass can be assigned  a reference to any subclass derived from that superclass. You will find this aspect of inheritance quite useful in a variety of situations.

And it is important to understand that it is the type of the reference variable --- not the type of the object that it refers to----that determines what members can be accessed. That is, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值