私有内部类调用
酷炫lv 2017-06-28 11:00:39 在一下代码中,使用了外部对象获取接口对象,再由接口调用f()方法,但是内部类定义为private,应该只有在OuterClass2中才可以调用,为什么在接口对象中可以调用??
//抽象类
interface OutInterface {
public void f();
}
// 外部类
class OuterClass2 {
// 内部类并且定义为私有,实现接口
private class InnerClass implements OutInterface {
InnerClass(String s) {
System.out.println(s);
}
// 重写
public void f() {
System.out.println("访问内部类中的f()方法");
}
}
public OutInterface doit() {
return new InnerClass("访问内部类构造方法");
}
}
// 主方法
public class InterfaceInner {
public static void main(String[] args) {
OuterClass2 out = new OuterClass2();
OutInterface outinter = out.doit();
outinter.f();
}
}