Java继承中的私有成员 [英] Private members in Java inheritance

查看:199
本文介绍了Java继承中的私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,对于Java子类,它可以继承其超类的所有成员。这甚至意味着私人会员呢?我知道它可以继承受保护的成员。

I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.

有人可以向我解释一下。我现在完全糊涂了。

Can someone explain this to me. I am now totally confused.

推荐答案

不,私人会员 没有继承 因为私有成员的范围仅限于到定义它的类。仅继承public和protected成员。

No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited.

来自 Java文档


私人会员超类

子类 不会继承其父类的
私有成员

但是,如果超类具有公共
或用于访问其
私有字段的受保护方法,则子类也可以使用
。嵌套类具有
访问
的所有私有成员的封闭类 - 两个字段和
方法。因此,
子类继承的public或
保护的嵌套类可以间接访问超类的私有成员的所有

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.

来自 JLS


声明为
private的类 的成员不会被该类的
子类继承
。只有声明受保护的
或public的类的成员
才会被声明在类的
之外的包中声明的子类
继承。

Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.

一个有用的链接:子类是否继承私有字段?

这篇关于Java继承中的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆