Java 受保护/公共内部类
有人能给我解释一下Java 受保护/公共内部类,java,inner-classes,access-modifiers,Java,Inner Classes,Access Modifiers,有人能给我解释一下受保护/公共内部类之间的区别吗 我知道,public内部类要尽可能避免(如本文所述) 但是从我所知道的,使用protected或public修饰符没有区别 看看这个例子: public class Foo1 { public Foo1() { } protected class InnerFoo { public InnerFoo() { super(); } } } 无论我声明InnerFooprotected还是public,所有这些都编译并有效
受保护
/公共
内部类之间的区别吗
我知道,public
内部类要尽可能避免(如本文所述)
但是从我所知道的,使用protected
或public
修饰符没有区别
看看这个例子:
public class Foo1 {
public Foo1() { }
protected class InnerFoo {
public InnerFoo() {
super();
}
}
}
无论我声明InnerFoo
protected
还是public
,所有这些都编译并有效
我错过了什么?请给我指出一个使用protected
或public
有区别的例子
谢谢。受保护的访问修饰符将限制来自同一软件包及其子类以外的类的访问 在所示示例中,
public
和protected
将具有相同的效果,因为它们位于同一个包中
有关访问修饰符的更多信息,请参阅的页。您可以认为受保护的内部类是受保护的成员,因此它只对类、包、子类进行访问,而不对整个世界进行访问 此外,对于outter类,它只有两个访问修饰符。只需公开和打包。java中的奇怪事情: 纯Java:不能从公共getter返回私有内部类 在JSP:中,不能从公共getter返回非公共内部类
您可以运行的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]
对于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>
${my_inst.get().myProperty()}
使用的堆栈:
Java8+Tomcat9正常。我明白我的困惑。因为内部类类似于封闭类的成员,所以我考虑的是受保护的成员。谢谢。布鲁诺康德:保护类就像保护成员。换句话说,爪哇中的“保护”,无论应用于成员还是内部类,都不同于C++和C.*。它授予对同一包(命名空间)中其他类的访问权。@Qwertie:party有点晚了。。。但是protectedinjava允许访问同一包中的其他类、声明类和所有子类(无论是否在同一包中)。然而,据我所知,在访问修饰符涉及的地方没有包层次结构。也就是说,以声明类的包名为前缀的包中的类将无权访问。
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]
<%@ 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>