有没有办法从第一类到第二类获取局部变量?第一类到第二类的局部变量
class Position:
positionX = 0.0 #starting value of positionX, but I need to change this in counting method when I push arrow key
def counting(self, posX):
self.positionX = posX #posX is for example position X of my cursor which I move with arrows so value is changing when I push to arrow key.
class Draw:
posInst = Position()
print posInst.positionX #here I need to get positionX variable from Position class. But its show me just 0.0. I need to get exact value which is change when I push arrow key and its change in counting method. If I push arrow key and value in counting method will be 20 I need this number in Draw class. But everytime is there 0.0.
有什么办法可以做到这一点?感谢您的建议。
Noes。 Python讨厌冗长,getter不可能成为Python中任何东西的“正确”方式。 – bobrobbob
我想你会发现,虽然更详细,但所有的内部变量都应该受到保护而不受外部操纵。这是针对任何OOP语言中的任何对象,任何更改都应通过函数调用进行,而不是直接进行 – TheMerovingian
我找到了解决方案。当我在Position类的构造函数__init__中使用它的工作。但它只在mainLoop中工作。 – Thomas