我正在尝试为我的私有 self.__food 变量创建一个 setter。基本上,我希望子类 Tiger 更改这个私有变量,并限制其值不超过 100。但是,我收到了一个错误:TypeError:'int' object is not callable。我错在哪里?如何解决?谢谢。
class Animal:
def __init__(self,foodamount=10, location = 'Australia'):
self.__food = foodamount
self.location = location
@property
def foodamt(self):
return self.__food
@foodamt.setter
def foodsetter(self, foodamount):
if self.__food >100:
self.__food = 100
else: self.__food = foodamount
class Tiger(Animal):
def __init__(self,colour = 'orange'):
super().__init__(location ='programming and gaming')
self.colour = colour
an = Tiger()
an.colour='red'
print(an.colour)
ansetfood = an.foodsetter(1000)
print(ansetfood)