//匿名内部类
如果方法的参数被定义为一个接口类型,那么就需要定义一个类来实现接口
interface Animal{
void shout();
}
public class Example{
public static void public static void main(String[] args) {
//定义匿名内部类作为参数传递给animalShout()方法
animalShout(new Animal{
//实现shout方法
public void shout(){
System.out.println("");
}
});
}
}
//定义静态方法animalShout()
public static void animalShout(Animal an){
an.shout();//调用传入对象an的shout()方法
}
匿名内部类是实现接口的简便写法 格式为 new 父类(参数列表)或父接口(){
//匿名内部类实现部分
}
如果方法的参数被定义为一个接口类型,那么就需要定义一个类来实现接口
interface Animal{
void shout();
}
public class Example{
public static void public static void main(String[] args) {
//定义匿名内部类作为参数传递给animalShout()方法
animalShout(new Animal{
//实现shout方法
public void shout(){
System.out.println("");
}
});
}
}
//定义静态方法animalShout()
public static void animalShout(Animal an){
an.shout();//调用传入对象an的shout()方法
}
匿名内部类是实现接口的简便写法 格式为 new 父类(参数列表)或父接口(){
//匿名内部类实现部分
}