匿名内部类的方法调用

/**
 * 匿名内部类的方法调用
 * @author Administrator
 *
 */
interface Inter{
	public abstract void show();
	public abstract void show2();
}
class OuterA{
	public void method(){
		/*
		new Inter(){
			public void show(){
				System.out.println("show");
			}
			public void show2(){
				System.out.println("show2");
			}
		}.show();
		new Inter(){
			public void show(){
				System.out.println("show");
			}
			public void show2(){
				System.out.println("show2");
			}
		}.show2();
		*/
		//优化版
		Inter i = new Inter(){//多态
			public void show(){
				System.out.println("show");
			}
			public void show2(){
				System.out.println("show2");
			}
		};
		i.show();
		i.show2();
	}
}
public class InterClass {
	public static void main(String[] args){
		OuterA o = new OuterA();
		o.method();
	}
}

猜你喜欢

转载自blog.csdn.net/weimeig/article/details/80193226