//先看一个内部类
interface Animal{
void shout();
}
public class Example22{
public static void main(String[] args){
class Cat implements Animal{ //使用内部类实现接口
public void shout(){
System.out.println("Miaowu……");
}
}//内部类结束
animalShout(new Cat());//new Cat创建对象,直接将将实例对象作为参数传入
}
public static void animalShout(Animal an){
an.shout();
}
}
interface Animal{
void shout();
}
public class Example23{
public static void main(String[] args){
animalShout(new Animal()//new Animal() 相当于创建了一个实例对象,并将对象传给animalShout方法
{//从{开始,表示创建Animal的子类实例,该子类是匿名的,即没有名字,直接整个做为参数传入animalShout中,不需要起名,省掉了上文的Cat
public void shout(){
System.out.println("Miao miao");
}
});//animalShout()在此结束,()中创建了对象,并为对象包藏了方法
}
public static void animalShout(Animal an){
an.shout();
}
}