내 개인 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)
몇 가지 문제가 있습니다.
an.foodsetter(1000)
. 와 같은 속성 할당 구문을 사용합니다 an.foodamt = 1000
. 이것이 속성의 전체 요점입니다. 함수와 같은 동작을 유지하면서 투명한 속성과 같은 구문을 갖는 것입니다.foodamount
아니라 100 과 비교해야합니다 self.__food
.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 foodamt(self, foodamount):
if foodamount >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 = Animal()
an.colour='red'
print(an.colour)
an.foodamt = 1000
print(an.foodamt)
결과:
red
100
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다