所谓的匿名类,就是没有名字的类
public class 匿名类 {
public static void main(String[] args) {
Me me = new Me();
//没有使用内部类的情况下
me.sayHello(new zhangsan()); //Hellozhangsan
//使用匿名类后
me.sayHello(new Person3() {
@Override
public String name() {
return "wangwu";
}
});
}
}
abstract class Person3 {
public abstract String name();
}
class Me {
public void sayHello(Person3 person3) {
System.out.println("Hello" + person3.name());
}
}
class zhangsan extends Person3 {
@Override
public String name() {
return "zhangsan";
}
}