您的位置:首页 > 其它

匿名内部类

2015-08-02 21:04 246 查看
How

new 父类或接口(){继承父类或者实现接口所需的内容}; //注意理解这里的分号

补全以下内容

interface Inter
{
void method();
}
class Test
{
//补全代码,要求通过内部类

}
class NiMingNeiBu
{
public static void main(String[] args)
{
Test.function().method();
}
}


分析

interface Inter
{
void method();
}
class Test
{
//静态 返回Inter
static Inter function(){
//返回Inter,而Inter是一个接口,不能被实例-》new Inter(){实现接口}<=>实现了接口Inter的匿名类
return new Inter()
{
public void method(){
System.out.println("欧了");
}
};
}

}
class NiMingNeiBu
{
public static void main(String[] args)
{
/*分析:
1:Test.function(),说明在Test内部定义了一个静态方法function()
2:.method()说明function返回的是一个实现了Inter的对象
*/
Test.function().method();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: