黑马程序员技术交流社区
标题:
匿名内部类
[打印本页]
作者:
安安安
时间:
2015-6-15 23:27
标题:
匿名内部类
刚才看见有人问匿名内部类。
就把笔记发出来下。
注释很详细就不多说了。
public class Demo1
{
public static void main(String[] args)
{
//Test.function():Test类中有一个静态的方法function.
//.method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
//因为只有Inter类型的对象(子类),才可以调用method。
Test.function().method();
// Inter in=Test.function();
// in.method();
//面试题
//无父类无借口写匿名内部类
new Object(){
void function(){
System.out.println("function");
}
}.function();
}
}
interface Inter
{
public abstract void method();
}
class Test
{
//补足代码。通过匿名内部类。
// static class Inner implements Inter
// {
// public void method(){
// System.out.println("method");
// }
// }
static Inter function(){
// return new Inner();
return new Inter()
{
public void method(){
System.out.println("method");
}
};
}
}
复制代码
作者:
金亮0602
时间:
2015-6-15 23:32
正在看,还要再琢磨一下,多谢分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2