为什么不能将类定义为受保护的? [英] Why can a class not be defined as protected?
问题描述
我知道这是一个愚蠢的问题,但我仍有疑问需要清除。
我的问题是,为什么我们不能将类定义为 protected
?
我知道我们不能,但为什么?应该有一些特定的原因。
因为没有意义。
受保护的类成员(方法或变量)就像package-private(默认可见性),除了它也可以从子类访问。
因为没有'subpackage'或'这样的概念Java中的package-inheritance',声明class protected或package-private是相同的。
你可以将嵌套和内部类声明为protected或private。 / p>
I know this is a stupid question, but I still have a doubt which needs to be cleared.
My question is, why can we not define a class as protected
?
I know that we can't, but why? There should be some specific reason.
Because it makes no sense.
Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses.
Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing.
You can declare nested and inner classes as protected or private, though.
这篇关于为什么不能将类定义为受保护的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!