受保护/公共内部类
有人可以向我解释一下 protected
/ public
Inner 类之间有什么区别吗?
我知道 public
内部类应尽可能避免(如文章)。
但据我所知,使用 protected
或 public
修饰符之间没有区别。
看一下这个例子:
public class Foo1 {
public Foo1() { }
protected class InnerFoo {
public InnerFoo() {
super();
}
}
}
...
public class Foo2 extends Foo1 {
public Foo2() {
Foo1.InnerFoo innerFoo = new Foo1.InnerFoo();
}
}
...
public class Bar {
public Bar() {
Foo1 foo1 = new Foo1();
Foo1.InnerFoo innerFoo1 = foo1.new InnerFoo();
Foo2 foo2 = new Foo2();
Foo2.InnerFoo innerFoo2 = foo2.new InnerFoo();
}
}
无论我声明 InnerFoo
protected
还是 public
,所有这些都会编译并且有效。
我缺少什么? 请指出使用 protected
或 public
之间存在差异的情况。
谢谢。
Can someone please explain to me what is the difference between protected
/ public
Inner classes?
I know that public
inner classes are to avoid as much as possible (like explained in this article).
But from what I can tell, there is no difference between using protected
or public
modifiers.
Take a look at this example:
public class Foo1 {
public Foo1() { }
protected class InnerFoo {
public InnerFoo() {
super();
}
}
}
...
public class Foo2 extends Foo1 {
public Foo2() {
Foo1.InnerFoo innerFoo = new Foo1.InnerFoo();
}
}
...
public class Bar {
public Bar() {
Foo1 foo1 = new Foo1();
Foo1.InnerFoo innerFoo1 = foo1.new InnerFoo();
Foo2 foo2 = new Foo2();
Foo2.InnerFoo innerFoo2 = foo2.new InnerFoo();
}
}
All of this compiles and is valid whether I declare InnerFoo
protected
or public
.
What am I missing? Please, point me out a case where there's a difference in using protected
or public
.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
protected
访问修饰符将限制来自同一包及其子类以外的类的访问。在所示示例中,
public
和protected
将具有相同的效果,因为它们位于同一包中。有关访问修饰符的详细信息,请参阅控制对类成员的访问Java 教程 的 页面可能会感兴趣。
The
protected
access modifier will restrict access from classes other than the ones in the same package and its subclasses.In the example shown, the
public
andprotected
will have the same effect, as they are in the same package.For more information on access modifiers, the Controlling Access to Members of a Class page of The Java Tutorials may be of interest.
你可以认为受保护的内部类是受保护的成员,所以它只能访问类、包、子类,但不能访问世界。
另外,对于外部类,它只有两个访问修饰符。 只是公开和打包。
You can just think protected inner class is protected member, so it only access for class, package, subclass but not for the world.
In addition, for outter class, there is only two access modifier for it. Just public and package.
java 中奇怪的事情:
纯 Java: 你不能从公共 getter 返回一个私有内部类。
在 JSP 中:您不能从公共 getter 返回非公共内部类。
您可以运行的Java演示:
对于JSP,仅将上面的类代码放入文件夹:com/myPackage/MyClass
并将“import com.myPackage.MyClass”作为源代码的第一行。 然后使用以下源代码创建一个新的 .jsp 页面:
使用的堆栈:
Java8 + Tomcat9
Weird thing in java:
Pure Java: You cannot return a private inner class from a public getter.
In JSP : You cannot return a non-public inner class from a public getter.
Java Demo You Can Run:
For JSP, put only the class code above into folder: com/myPackage/MyClass
and put "import com.myPackage.MyClass" as first line of source code. Then create a new .jsp page with this source code:
Stack Used:
Java8 + Tomcat9