------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
首先看下面这道题,这是一个练习内部类的测试实例,补全代码,通过代码的补全,了解内部类的情况
其中Inter是一个接口类,Test是一个待补全的类,InnerClass是一个测试运行类
interface Inter
{
void method();
}
class Test
{
//在此处补全代码
}
public class InnerClassTest
{
public static void main(String[] args)
{
Test.function().method();
}
}
根据以上代码可知,在main方法中运行的语句 Test.function().method();
进行分解,Test.function(): 可以知道Test类中有一个静态的方法function
而且该方法返回的内容可以直接调用.method方法,则这个方法运算后返回的是一个对象
而且是Inter类型的对象,因为只有Inter类型的对象,才可以调用method方法
因此,应补全的代码如下:
static Inter function()
{
return new Inter()
{
public void method()
{
System.out.println("Inter method run");
}
};
}