package com.cn.test;
//匿名内部类,就是一个对象
//匿名对象只能调用一次方法
interface Inter{
public abstract void show();
public abstract void show2();
}
class outer1{
public void method(){
/*//一个方法的时候
new Inter(){//此处是一个对象
public void show(){//方法的重写
System.out.println("show");//此处是一个对象,没有调用show方法,不会输出结果Inner i=new Inner(); 红色的和蓝色的内容相等 对象的后面是一个分号
}
//}; //此处是一个对象
}.show();//此处是方法的调用
*/
////两个方法的时候
//重复调用麻烦
/*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();//对象的调用
*/ //方法的改进-------------多态的改进
Inter i=new Inter(){//多态的实现
public void show(){
System.out.println("show");
}
public void show2(){
System.out.println("show2");
}
};
i.show();
i.show2();
}
}
public class nimingInner {
public static void main(String[] args){
outer1 o=new outer1();
o.method();
}
}
//匿名内部类,就是一个对象
//匿名对象只能调用一次方法
interface Inter{
public abstract void show();
public abstract void show2();
}
class outer1{
public void method(){
/*//一个方法的时候
new Inter(){//此处是一个对象
public void show(){//方法的重写
System.out.println("show");//此处是一个对象,没有调用show方法,不会输出结果Inner i=new Inner(); 红色的和蓝色的内容相等 对象的后面是一个分号
}
//}; //此处是一个对象
}.show();//此处是方法的调用
*/
////两个方法的时候
//重复调用麻烦
/*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();//对象的调用
*/ //方法的改进-------------多态的改进
Inter i=new Inter(){//多态的实现
public void show(){
System.out.println("show");
}
public void show2(){
System.out.println("show2");
}
};
i.show();
i.show2();
}
}
public class nimingInner {
public static void main(String[] args){
outer1 o=new outer1();
o.method();
}
}