匿名内部类的简单使用
1. new 一个抽象的类:
package com.xuzhiwen.test1; abstract class Person { abstract void eat(); }
package com.xuzhiwen.test1; public class TestPerson { public static void main(String[] args) { Person p = new Person() { @Override void eat() { System.out.println("i can eat"); } }; p.eat(); } }
2. new 一个接口:
package com.xuzhiwen.test1; public interface Animal { public void run(); }
package com.xuzhiwen.test1; public class Dog { public static void main(String[] args) { new Animal() { @Override public void run() { System.out.println("run().."); } public void getName(){ System.out.println("getName()..."); } }.getName(); } }