关于继承

在网上看到一个关于继承的段子:
”老爹将毕生所学的技能传授于儿子,儿子在“继承”到了,便可以使用了。儿子所使用的技能是他自己从他老子那里学来的,不是他老子的。有时候,老爹觉得有些技能“不好”或者是儿子目前所不需要的,那么不打算把这些技能传授于儿子(private)。因此,儿子是不会知道如何使用这些技能的。不过,儿子有时候也会觉得老子某些技能不好用,然后加以改进(覆盖)。当然,儿子没准也会自创一些技能,这些技能是他老子都不会的。“
这个段子比较形象,适合记忆。
附一段上java官方文档关于能不能继承private的文字:
Private Members in a Superclass
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
这段话第一句就说了子类不能继承父类的私有成员。
但是这里所说的不能继承,意思是不能直接用sonclass.privatefather这种形式来调用父类的成员,但是可以通过父类public来间接访问其private成员。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值