Java 受保护/公共内部类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/595179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
protected/public Inner Classes
提问by bruno conde
Can someone please explain to me what is the difference between protected
/ public
Innerclasses?
有人可以向我解释protected
/public
内部类之间有什么区别吗?
I know that public
inner classes are to avoid as much as possible (like explained in this article).
我知道public
内部类是尽可能避免的(就像在这篇文章中解释的那样)。
But from what I can tell, there is no difference between using protected
or public
modifiers.
但据我所知,使用protected
或public
修饰符之间没有区别。
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
.
所有这些都可以编译并且无论我声明InnerFoo
protected
还是public
.
What am I missing? Please, point me out a case where there's a difference in using protected
or public
.
我错过了什么?请指出使用protected
or有区别的情况public
。
Thanks.
谢谢。
采纳答案by coobird
The protected
access modifier will restrict access from classes other than the ones in the same package and its subclasses.
该protected
访问修饰符将限制从比在同一个包及其子类之外的其他类的访问。
In the example shown, the public
and protected
will have the same effect, as they are in the same package.
在显示的示例中,public
和protected
将具有相同的效果,因为它们在同一个包中。
For more information on access modifiers, the Controlling Access to Members of a Classpage of The Java Tutorialsmay be of interest.
有关访问修饰符的更多信息,您可能会对The Java Tutorials的Controlling Access to Member of a Class页面感兴趣。
回答by wangzhengyi
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.
此外,对于外部类,它只有两个访问修饰符。只是公开和打包。
回答by J.M.I. MADISON
Weird thing in java:
Java中的奇怪事情:
Pure Java:You cannot return a private inner classfrom a public getter.
纯 Java:您不能从公共 getter返回私有内部类。
In JSP :You cannot return a non-public inner classfrom a public getter.
在 JSP 中:您不能从公共 getter返回非公共内部类。
Java Demo You Can Run:
您可以运行的 Java 演示:
public class ReturnInnerClass{
public static void main(String []args){
MyClass inst = new MyClass("[PROP_VAL]");
System.out.println(
inst.get().myProperty()
);;
};;
};;
class MyClass{
//:If JSP: MUST be public
//:Pure Java:
//: public,protected,no-access-modifier
//: Will all work.
//:Private fails in both pure java & jsp.
protected class Getters{
public String
myProperty(){ return(my_property); }
};;
//:JSP EL can only access functions:
private Getters _get;
public Getters get(){ return _get; }
private String
my_property;
public MyClass(String my_property){
super();
this.my_property = my_property;
_get = new Getters();
};;
};;
//:How to run this example:
//: 1: Put this code in file called: "ReturnInnerClass.java"
//: 2: Put ReturnInnerClass.java into it's own folder.
//: ( Folder name does not matter.)
//: 3: Open the folder.
//: 4: Right-Click --> GitBashHere
//: 5: In command prompt within folder:
//: 5.1: javac ReturnInnerClass.java
//: 5.2: java ReturnInnerClass
//: ( javac: java compiler )
//: ( java : runs compiled java program )
//: EXPECTED OUTPUT:
//: [PROP_VAL]
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:
对于 JSP,只将上面的类代码放入文件夹:com/myPackage/MyClass 并将“import com.myPackage.MyClass”作为源代码的第一行。然后使用以下源代码创建一个新的 .jsp 页面:
<%@ taglib uri ="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.myPackage.MyClass" %>
<%
MyClass inst = new MyClass("[PROP_VALUE]");
pageContext.setAttribute("my_inst", inst );
%><html lang="en"><body>
${ my_inst.get().myProperty() }
</body></html>
Stack Used:Java8 + Tomcat9
使用的堆栈:Java8 + Tomcat9