public class AnonymousInnerClass {
public static void main(String[] args) {
Person p = new Person();
// Animal dog=new Dog();
// p.feed(dog);
new Animal() {
private String name="abl";
/* static{
name="ss";
}Cannot make a static reference to the non-static field name*/
public void eat() {
System.out.println("啃骨头");
}
public void show() {
System.out.println(name);
}
}.show();
//p.feed(dog);
//p.feed(dog);
p.feed(new Animal() {
public void eat() {
System.out.println("吃鱼肉");
}
});
}
}
class Person {
public void feed(Animal animal) {
animal.eat();
}
}
interface Animal{
public abstract void eat();
}
//abstract class Animal {
// public abstract void eat();
//}
/*
* class Dog extends Animal{
* public void eat(){
* System.out.println("啃骨头"); }
* }
*/
结果是——