0
在程序运行时,我会如何向列表中添加附加的locationx和位置变量?类的如何向类添加其他属性?
例子:
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。
请注意,您的标题有误导之嫌。你根本没有添加新的属性。相反,你只需要将'.append()'列表。 –
'building_shipyard.locationx.append(7000)' –