面向对象_匿名内部类的面试题
原创
©著作权归作者所有:来自51CTO博客作者wx62dfdc6aea345的原创作品,请联系作者获取转载授权,否则将追究法律责任
/*
按照要求,补齐代码
interface Inter{
void sho();
}
class Outer{
//补齐代码
}
class OuterDemo{
public static void main(String[] args){
Outer.method().show();
}
}
要求在控制台输出"HelloWorld"
*/
package cn.test;
interface Inter{
void show();
//public abstract void show();
}
class Outer{
public static Inter method(){
//子类对象 -- 子类匿名对象
return new Inter(){
public void show(){
System.out.println("HelloWorld");
}
};
}
}
class OuterDemo{
public static void main(String[] args){
Outer.method().show();
/*
a:Outer.method()可以看method()应该是Outer()中的一个静态方法?
b:Outer.method().show()可以看method()方法的返回值是一个对象。
又由于接口Inter中有一个show()方法,所以我认为method()方法的返回值类型是一个接口。
*/
}
}