匿名内部类就是内部类的简化写法
前提:存在一个类或者接口,这里的类可以是具体类也可以是抽象类
格式: new 类名或者接口名(){
重写方法;
}
其本质就是一个继承了该类或者实现了该接口的子类匿名对象
//11111111111111111111111
interface Inter{
public abstract void show();
}
class Outer{
public void method(){
new Inter(){
public void show(){
System.out.println("show");
}
}.show();
}
}
class Inner{
public static void main(String[] args){
Outer o=new Outer();
o.method();
}
}
//2222222222222222222
interface Inter{
public abstract void show();
public abstract void show2();
}
class Outer{
public void method(){
new Inter(){
public void show(){
System.out.println("show");
}
public void show2(){
System.out.println("show2");
}
}.show();
new Inter(){
public void show(){
System.out.println("show");
}
public void show2(){
System.out.println("show2");
}
}.show2();
}
}
class Inner{
public static void main(String[] args){
Outer o=new Outer();
o.method();
}
}
//333333333333333
interface Inter{
public abstract void show();
public abstract void show2();
}
class Outer{
public void method(){
Inter i=new Inter(){
public void show(){
System.out.println("show");
}
public void show2(){
System.out.println("show2");
}
};
i.show();
i.show2();
}
}
class Inner{
public static void main(String[] args){
Outer o=new Outer();
o.method();
}
}