笔记如下,自己编写时出现的问题在最后: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();所以程序出错
复制代码
|
|