输出不能看明白,要是能树状排序就好了
# coding=utf-8
# 图形化
class Graphic:
# 显示
def render(self):
raise NotImplementedError("你应该实现这个方法.")
# 复合图形
class CompositeGraphic(Graphic):
def __init__(self):
self.graphics = []
# 显示
def render(self):
for graphic in self.graphics:
graphic.render( )
# 添加
def add(self, graphic):
self.graphics.append(graphic)
# 删除
def remove(self, graphic):
self.graphics.remove(graphic)
# 椭圆
class Ellipse(Graphic):
def __init__(self, name):
self.name = name
def render(self):
print(self.name)
if __name__ == '__main__':
ellipse1 = Ellipse("椭圆 1")
ellipse2 = Ellipse("椭圆 2")
ellipse3 = Ellipse("椭圆 3")
graphic1 = CompositeGraphic( )
graphic1.add(ellipse1)
graphic1.add(ellipse2)
graphic1.add(ellipse3)
graphic2 = CompositeGraphic( )
ellipse4 = Ellipse("椭圆 4")
graphic2.add(ellipse4)
graphic = CompositeGraphic( )
graphic.add(graphic1)
graphic.add(graphic2)
graphic.render( )
输出:
椭圆 1
椭圆 2
椭圆 3
椭圆 4