簡體   English   中英

如何向類添加其他屬性?

[英]How to add additional attributes to a class?

程序運行時,如何在列表中添加其他locationx和locationy變量?

該類的示例:

class Building:
    'Common base class for all buildings'

    def __init__(self, name, hp, img, locationx, locationy, height, width):
        self.name = name
        self.hp = hp
        self.img = img
        self.locationx = locationx
        self.locationy = locationy
        self.height = height
        self.width = width

building_shipyard = Building("Shipyard",500,shipyard,[1000,2500,5000],[1000,2500,5000],300,300)

例如,在用戶放置新建築物之后,我可能希望將self.locationx和self.locationy從[1000,2500,5000]更新為[1000,2500,5000,7000]。

只需訪問以下屬性:

...
def place_building ():
    global building_shipyard 
    ...
    building_shipyard.locationx =  [1000,2500,5000,7000]
    # or
    building_shipyard.locationx.append(7000)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM