我看那个内部匿名类的写法(代码1),接口可以直接new,代码2中抽象类可以直接NEW,那是否可以直接 new 一个普通类然后复写其中方法比如代码3?
代码1
interface Person {
public void eat();
}
public class Demo {
public static void main(String[] args) {
Person p = new Person() {
public void eat() {
System.out.println("eat something");
}
};
p.eat();
}
}
运行结果:eat something
代码2
abstract class Person {
public abstract void eat();
}
public class Demo {
public static void main(String[] args) {
Person p = new Person() {
public void eat() {
System.out.println("eat something");
}
};
p.eat();
}
}
代码3
class Person {
public abstract void eat(){
System.out.println("eat ");
};
}
public class Demo {
public static void main(String[] args) {
Person p = new Person() {
public void eat() {
System.out.println("eat again again?");
}
};
p.eat();
}
}
RISEBY
汪汪一只猫
慕雪6442864
慕无忌1623718
UYOU
相关分类