Java访问修饰符的可访问范围 [英] Accessibility scope of Java access modifiers

查看:123
本文介绍了Java访问修饰符的可访问范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java具有私有,受保护和公共访问修饰符。您能否解释这些修饰符的可访问范围。

Java has private, protected, and public access modifiers. Can you explain the accessibility scope of these modifiers.

如何访问其他程序包中的受保护成员?

How can I access a protected member within a different package?

推荐答案

为更好地理解,您需要查看此

For better understanding you need to see this

Access Modifiers

                   Same Class      Same Package            Subclass     Other packages
public               Y                Y                      Y                   Y
protected            Y                Y                      Y                   N
no access modifier   Y                Y                      N                   N
private               Y               N                      N                   N


这里的重要区别是 Default 受保护
默认值:在程序包外部无法访问
受保护:仅在程序包外部可以访问,如果仅当该类是子类时才找到
请参阅此获取更多详细信息。
编辑:由于您的问题的答案也与 相同,您可以通过将您的类设为子类来访问受保护的成员该类的类,其中定义了受保护的成员


Here the important difference is between Default and protected.
Default: Never accessible outside the package
Protected: Only accessible outside the package, if and only if the class is a subclass
Please see this for further details.
As your question's answer is also the same that You can access the protected member by make your class a sub class of the class , in which protected member is defined

这篇关于Java访问修饰符的可访问范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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