interface Inter {
public abstract void show1();
public abstract void show2();
}
//有名字的内部类
class Outer {
public void method(){
class Inner implements Inter {
public void show1(){
System.out.println("show1");
}
public void show2(){
System.out.println("show1");
}
}
}
public void show() {
Inter in = new Inner();//父类引用指向子类对象
in.show1();
in.show2();
}
}
//匿名内部类(重点,android里面用的多,awt的监听器用的多)
class Outer {
public void method() {
//匿名内部类对象调用方法
new Inter() {
public void show1() {
System.out.println("show1");
}
public void show2() {
System.out.println("show1");
}
}.show1();
new Inter() {
public void show1() {
System.out.println("show1");
}
public void show2(){
System.out.println("show1");
}
}.show2();
//匿名内部类对象的另一种调用方式,父类引用指向子类对象
Inter in = new Inter() {
public void show1() {
System.out.println("show1");
}