使用组合关系来实现复用时,要创建两个对象,是不是开销更大?
class Ye
{
private void breath()
{
System.out.println("breathing");
}
}
class Fu extends Ye
{
private Ye a;
Fu(Ye a)
{
this.a=a;
}
public void breath()
{
a.breath();
}
public void work()
{
System.out.println("working");
}
}
class Zi extends Fu
{
private Ye a;
Zi(Ye a)
{
this.a=a;
}
public void breath()
{
a.breath()
}
public void study()
{
System.out.println("studying");
}
}
class ExtendsDemo4
{
public static void main(String[] args)
{
Ye a1=new Ye();
Fu b=new Fu(a1);
b.breath();
b.work();
Ye a2=new Ye();
Zi c=new Zi(a2);
c.breath();
c.study();
}
}
|
|