黑马程序员技术交流社区

标题: 当用组合关系来实现复用是不是开销更大 [打印本页]

作者: 孙天    时间: 2012-4-26 12:26
标题: 当用组合关系来实现复用是不是开销更大
使用组合关系来实现复用时,要创建两个对象,是不是开销更大?
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();
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2