2013-03-07 32 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

你有什么期待被退回? – 2013-03-09 20:36:27

回答

0

好吧,让我们从Coin Collector开始。他coin属性被设置为0。 你也有一个updatevisit功能的第二类。

更新功能在每次调用时创建一个新的coin_collector。然后它将visit函数的返回值分配给self.c_Count。现在让我们看看visit函数。

这需要在新coin collector品牌,并返回这将为其赋值参数为coin领域,如果你会通过TrueTF

coin_count函数返回后,您不会在visit函数中执行任何操作,因此返回值将丢失。这就是为什么当你尝试分配self.visit的结果时,你会得到一个None