黑马程序员技术交流社区
标题:
基础视频匿名类测试题笔记,有一个小问题,我理解对吗?
[打印本页]
作者:
David.L
时间:
2014-7-20 12:53
标题:
基础视频匿名类测试题笔记,有一个小问题,我理解对吗?
笔记如下,自己编写时出现的问题在最后:L ,应该是个小错误,不过可能是我写的代码少吧
interface Inter
{
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("NiMing");
}
};
}
}
class InnerClassDemoTest
{
public static void main(String[] args)
{
//Test.function():Test类中有一个静态方法function
//.method():function这个方法运算后的结果是一个对象,而且是一个Inter类型的对象。
//因为只有是Inter类型的对象,才可以调用method方法
Test.function().method();
Inter in=Test.function();
in.method();
}
}
//在static Inter function() 返回Inter上犯错,写成void,同时没有return,就没有把对象地址返回给main函数
//这就使Test.function().method();变成了null.method();所以程序出错
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2