管理(和重命名)类组合物的实例变量

问题描述 投票:2回答:1

我想提出一个类组成,使该组成的类的实例变量成为的成分,但与调整名称实例变量。

这个应用程序是在确定在matplotlib绘制新对象。一个例子是,我想有一个功能drawMyArrow绘制具有可能不同的颜色(和其他规格)为它的头部,尾部,和电弧的箭头。我希望能够通过关键字参数来传递各种规格的头,尾和弧drawMyArrow。我没有带班工作过,但在这个在线阅读,我认为,要解决我的问题的最好的方法是定义一个类MyArrow是一些类ArrowHeadArrowArc的组成。

为了说明我的问题,考虑一个简单的玩具例子。让我们定义一个类Room是类wallwindowdoor的组成。

class Door:
    def __init__(self, color='white', height=2.3, width=1.0):
        self.color = color
        self.height = height
        self.width = width

class Window:
    def __init__(self, color='white', height=1.0, width=0.8):
        self.color = color
        self.height = height
        self.width = width

class Wall:
    def __init__(self, color='white', height=2.5, width=4.0):
        self.color = color
        self.height = height
        self.width = width

class Room:
    def __init__(self):
        self.door = Door()
        self.window = Window()
        self.wall = Wall()

DoorWindow的实例变量,并Wallcolorheightwidth。我想Room有实例变量doorcolorwindowcolorwallcolordoorheightwindowheight,等我可以添加所有九个实例变量明确Room并为它们定义setget功能。但是,如果我决定以后添加更多的实例变量DoorWindow,或Wall我总是需要太再次编辑Room的代码。有没有一种方法,使其采用(并重命名)的实例变量从它的组件类自动编写Room

python python-3.x class composition
1个回答
3
投票

您正在使用成分 - 无需重复存取您的会员。您可以轻松地通过您的成员组成访问属性:

r = Room()
print( r.window.color ) # to print the windows color only

你可能会从一个基类的“零件”和改变__init__(..)Room类,但是利润:

class Thing:
    """Base class handling init including a name and __str__ and __repr__."""
    def __init__(self, name, color, height, width):
        self.name = name
        self.color = color
        self.height = height
        self.width = width

    def __str__(self):
        return repr(self)

    def __repr__(self):
        return str([self.name, self.color, self.height, self.width])

class Door(Thing):
    def __init__(self, color='white', height=2.3, width=1.0):
        super(self.__class__, self).__init__(self.__class__.__name__, color, height, width)

class Window(Thing):
    def __init__(self, color='white', height=2.3, width=1.0):
        super(self.__class__, self).__init__(self.__class__.__name__, color, height, width)

class Wall(Thing):
    def __init__(self, color='white', height=2.5, width=4.0):
        super(self.__class__, self).__init__(self.__class__.__name__, color, height, width) 

class Room:
    def __init__(self,*, door=None, window=None, wall=None): # named params only
        self.door = door or Door()           # default to booring Door if none provided
        self.window = window or Window()     # same for Window
        self.wall = wall or Wall()           # same for Wall

    def __str__(self):
        return str([self.door,self.window,self.wall])

创建对象,并打印出来:

r = Room()
r2 = Room(window=Window("yellow"))

print(r)
print(r2)

r3 = Room( window=Window("green",0.5,0.5), door=Door("black",5,5), 
           wall=Wall("unicorncolored",5,5) )

print(r3)

输出:

# r - the cheap Room - using default-ing Things
[['Door', 'white', 2.3, 1.0], ['Window', 'white', 2.3, 1.0], ['Wall', 'white', 2.5, 4.0]]

# r2 - with a custom yellow Window
[['Door', 'white', 2.3, 1.0], ['Window', 'yellow', 2.3, 1.0], ['Wall', 'white', 2.5, 4.0]]

# r3 - all custom - 
[['Door', 'black', 5, 5], ['Window', 'green', 0.5, 0.5], ['Wall', 'unicorncolored', 5, 5]]
© www.soinside.com 2019 - 2024. All rights reserved.