私有类一般是内部类,不允许在外部创建该类对象public class Test { public static void main(String [] args){ new demo().new A();//true new demo().new B(); //false } }class demo { public class A { // do something } private class B { // do something } }
package est.net; public class Test5 { public static void main(String[] args) { A a = new A(); A.B b = a.getB(); a.getB().f(); a.receiveB(a.getB()); A.B b=a.new BImpl(); A.BImpl bImpl = a.new BImpl(); bImpl.f(); print("" + (a.getB() instanceof A.BImpl)); } private static void print(String msg) { System.out.println(msg); } }class A { private interface B { void f(); } public class BImpl implements B { public void f() { System.out.println("hello"); } } public B getB() { return new BImpl(); } public void receiveB(B b) { b.f(); } } B是私有的不能再类外引用;
private类的含义是使类拥有一个类成员变量,which能拥有类的一些feature。 我想了个使用情况,工作中我从没这样使用过,但我觉得这样写还可以,你只看看就可以了。public class A{ private interface I{void dosth();} private class B implements I{public void dosth(){}} private class C implements I{public void dosth(){}} private class D implements I{public void dosth(){}} public static void outwardInterface(int type){ I i = null; switch(type){ case 0:i = new A(). new B(); case 1:i = new A(). new C(); case 2:i=new A(). new D(); default:i=new A(). new B(); } i.dosth(); } }
所以你在Test这个类去访问 A.B b = a.getB();会出错
private interface 木有见过。
new demo().new A();//true
new demo().new B(); //false
}
}class demo {
public class A {
// do something
} private class B {
// do something
}
}
public类可以被其他包使用,而默认的只能在自己包里使用。其次,好像有private类,是实现内部类的,具体还没学到。
public class Test5 { public static void main(String[] args) {
A a = new A();
A.B b = a.getB();
a.getB().f();
a.receiveB(a.getB());
A.B b=a.new BImpl();
A.BImpl bImpl = a.new BImpl();
bImpl.f();
print("" + (a.getB() instanceof A.BImpl));
} private static void print(String msg) {
System.out.println(msg);
}
}class A {
private interface B {
void f();
} public class BImpl implements B {
public void f() {
System.out.println("hello");
}
} public B getB() {
return new BImpl();
} public void receiveB(B b) {
b.f();
}
}
B是私有的不能再类外引用;
我想了个使用情况,工作中我从没这样使用过,但我觉得这样写还可以,你只看看就可以了。public class A{
private interface I{void dosth();}
private class B implements I{public void dosth(){}}
private class C implements I{public void dosth(){}}
private class D implements I{public void dosth(){}}
public static void outwardInterface(int type){
I i = null;
switch(type){
case 0:i = new A(). new B();
case 1:i = new A(). new C();
case 2:i=new A(). new D();
default:i=new A(). new B();
}
i.dosth();
}
}