我会用牵强的微笑,活出无人能比的骄傲
分类: Java
2014-12-04 10:18:49
原文地址:java之路,基础知识----内部类 作者:一生有你llx
interface Machine
{
void run();
}
class Person
{
void run()
{
System.out.println("person run");
}
}
class Robot extends Person
{
private class MachineHeart implements Machine
{
public void run()
{
System.out.println("robot heart");
}
}
Machine getMachine()
{
return new MachineHerat();
}
}
class Test
{
public void main(String[] args)
{
Robot robot = new Robot();
Machine m = robot.getMachine();
m.run();
robot.run();
}
}
class A
{
void f1();
}
abstract B
{
abstract void f2();
}
class C extends A
{
B getB()
{
return new B() //返回一个内置类
{
public void f2()
{}
};
}
}
class Test
{
static void m1(A a)
{
a.f1();
}
static void m2(B b)
{
b.f2();
}
public static void main(String[] args)
{
C c = new C();
m1(c);
m2(c.getB());
}
}