/*
匿名内部类的方法调用
*/
=================单个方法调用 练习一 =========================
interface Inter{
public abstract void show(); } class Outer{ public void see(){ final int num = 10; new Inter(){ public void show(){ System.out.println(num); } }.show(); } } class Test{ public static void main(String args[]) { Outer oi =new Outer(); oi.see(); } } 结果:
10
======================多个方法调用,练习一 ============= interface Inter{
public abstract void show(); public abstract void show2(); } class Outer{ public void see(){ final int num = 10; new Inter(){ public void show(){ System.out.println(num); } public void show2(){ System.out.println("show2"); } }.show(); new Inter(){ public void show(){ System.out.println(num); } public void show2(){ System.out.println("show2"); } }.show2(); } } class Test{ public static void main(String args[]) { Outer oi =new Outer(); oi.see(); } } 结果:
10
show2 ==================多个方法的调用,简化版 练习二==========
interface Inter{
public abstract void show(); public abstract void show2(); } class Outer{ public void see(){ final int num = 10; Inter i =new Inter(){ //多态 public void show(){ System.out.println(num); } public void show2(){ System.out.println("show2"); } }; i.show(); i.show2(); } } class Test{ public static void main(String args[]) { Outer oi =new Outer(); oi.see(); } } 结果:
10
show2 |
|