/*
interface Inter
{
void method();
}
class Test
{
//补足代码,通过匿名内部类。
}
class InnerClassTest
{
public static void main(String[] args)
{
Test.funtion().method();
}
}
*/
//用内部类实现
/*
interface Inter
{
void method();
}
class Test
{
static class Inner implements Inter
{
public void method()
{
System.out.println("method run")
}
}
static Inter function()
{
return new Inner();
}
}
class InnerClassTest
{
public static void main(String[] args)
{
Test.funtion().method();
}
}
*/
//用匿名内部类实现
interface Inter
{
void method();
}
class Test
{
static Inter function()
{
return new Inter()
{
public void method()
{
System.out.println("method run");
}
};
}
}
class InnerClassTest
{
public static void main(String[] args)
{
//Test.function():看到这个可以知道Test类中有一个静态的方法function();
//.method():说明function()运算后得到的是一个对象,而且是一个Inter类型的对象。
//因为只有是Inter类型的对象,才可以调用method方法!
Test.function().method();
}
}
|
|