配合接口的使用,隐藏了具体实现类
public interface IInner {
void say();
}
public class TestCls {
String name = "ych";
public IInner getInner1() {
return new Inner();
}
public IInner getInner2() {
return new IInner() {
public void say() {
System.out.println("Hello2 " + name);
}
};
}
private class Inner implements IInner {
public void say() {
System.out.println("Hello1 " + name);
}
}
}
public class Application {
public static void main(String[] args) {
TestCls testCls = new TestCls();
IInner inner1 = testCls.getInner1();
inner1.say();
IInner inner2 = testCls.getInner2();
inner2.say();
}
}
原文:https://www.cnblogs.com/yinchh/p/11729784.html