显而易见的是,普通类的默认访问修饰符是包私有 它是内部本地类或内部匿名的私有内容 但是当我们尝试从容器类外部看到它时,会员类就像私有成员一样,这对我来说并不明显 私有还是包私有?
答案 0 :(得分:2)
除非另有指定,否则所有内容都具有默认访问权限(包为私有)。
(默认情况下一切都是公开的接口除外)
答案 1 :(得分:1)
与Tim B一样,默认情况下,所有内容都是包私有,但接口除外。
以下代码段演示了这一点。
public class OuterClass {
@Override
public String toString() {
return "I am the public outer class";
}
static class StaticInnerClass {
@Override
public String toString() {
return "I am the package private inner class";
}
}
class NonStaticInnerClass {
@Override
public String toString() {
return "I am the package private non static inner class";
}
}
public static void main(String[] args) {
OuterClass oc = new OuterClass();
OuterClass.StaticInnerClass ic = new OuterClass.StaticInnerClass();
NonStaticInnerClass nsic = oc.new NonStaticInnerClass();
System.out.println(oc);
System.out.println(ic);
System.out.println(nsic);
System.out.println(new SecondClass());
}
}
class SecondClass {
@Override
public String toString() {
OuterClass oc = new OuterClass();
OuterClass.StaticInnerClass ic = new OuterClass.StaticInnerClass();
OuterClass.NonStaticInnerClass nsic = oc.new NonStaticInnerClass();
return oc.toString() + "\n" + ic.toString() + "\n" + nsic.toString();
}
}