我的理解 匿名内部类 一般用在 实现接口,和重写父类方法时候用。目的就是减少代码量。
例如:
public class Test1{
public static void main(String[] args) {
//匿名内部类实现接口
AA show = new AA(){public void show(){System.out.println("hello");}};
show.show();
}
}
interface AA{
void show();
}
而我刚才突然想了个问题,能不能在不是接口,或父类上使用那
就随便写了这样的代码public class Test1{
public static void main(String[] args) { BB bb = new BB(){public void show(){
System.out.println("Hello World");
}};
}
}class BB {
}结果语法上是没有错.可是怎么调用这个方法那?
bb.show();会报错地
例如:
public class Test1{
public static void main(String[] args) {
//匿名内部类实现接口
AA show = new AA(){public void show(){System.out.println("hello");}};
show.show();
}
}
interface AA{
void show();
}
而我刚才突然想了个问题,能不能在不是接口,或父类上使用那
就随便写了这样的代码public class Test1{
public static void main(String[] args) { BB bb = new BB(){public void show(){
System.out.println("Hello World");
}};
}
}class BB {
}结果语法上是没有错.可是怎么调用这个方法那?
bb.show();会报错地
我只是好奇,所以乱写了下
如果都定义了接口方法,又何必用反射?!