匿名类(anonymous clss):定义一个实现该接口的类,并且假设基于该类的对象仅有一个被创建,则可使用下列形式。
Inter ob=new Inter(){*};
我们把“{*}”称为匿名类。
但下面的例子不是创建了基于该类的两个对象ob1,ob2吗?这怎么解释呢?
interface Inter{
public void hi();
}
public class Anony1{
public static void main(String[] args){
Inter ob1=new Inter(){
System.out.println("你好");
}
};
Inter ob2=new Inter(){
public void hi(){
System.out.println("hello");
}
};
ob1.hi();
ob2.hi();
}
}