2013-03-07 4 views
0

방문자 패턴을 사용하여 첫 번째 클래스에서 self.coin 값을 가져 와서 두 번째 클래스의 메서드로 반환하지만 작동하지 않는 경우 항상 반환 없음 ... 아무도 도와 줄 수 있습니까?pygame - 방문자 패턴을 가진 변수를 가져 오는 파이썬

class coin_collector(Observer): 
    def __init__(self): 
     super(Observer, self).__init__() 
     self.coin_count = 0 
     self.run = True 
     self.coin = 0 

    def acceptVisitor(self, visitor): 
     visitor.visit(self) 

    def update(self, observable, other): 
     me = coin_collector() 
     me.coin_Count(other, True) 

    def coin_Count(self, value, TF): 
     run = TF 
     if run: 
      self.coin = value 
      print self.coin 
     return self.coin 

    def __str__(self): 
     return self.__class__.__name__ 

#this is part of a different class in a different file 
    def visit(self, location): 
     location.coin_Count(0, False) 

    def update(self): 
     visitee = coin_collector() 
     self.c_Count = self.visit(visitee) # for some reason this always returns none 
     print self.c_Count, "working" # this always prints none... 
+0

반환 할 것으로 예상되는 것은 무엇입니까? –

답변

0

이제는 Coin Collector부터 시작하겠습니다. 그의 coin 속성은 0으로 설정됩니다. updatevisit 기능을 가진 두 번째 클래스가 있습니다.

업데이트 기능은 모든 호출마다 coin_collector을 만듭니다. 그런 다음 visit의 반환 값을 self.c_Count에 할당합니다. 이제 visit 함수를 살펴 보겠습니다.

그것은 coin collector 새로운 브랜드에 걸리고 TFTrue을 통과 할 경우 없음는 그것은 coin 필드 값 매개 변수를 할당하지 않을 것이다 반환합니다.

coin_count 함수에서 돌아 오면 visit 함수에서 아무 것도 수행하지 않으므로 반환 값이 손실됩니다. 따라서 self.visit의 결과를 할당하려고 시도하면 None이 표시됩니다.