给你写个简单Demo,匿名类的话要有个继承接口或者是继承抽象类
//抽象类
abstract class InClass {
public abstract void eat();
}
//定义Demo类
class Demo
{ //匿名内部类
InClass in=new InClass(){
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.print("我是匿名内部类");
}
};
}
//测试
public class Test12 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Demo demo=new Demo();
demo.person.eat();
}
}
|