Skip to main content
 首页 » 编程设计

来自另一个类实例的python类实例方法调用

2025年05月04日14xxx_UU

我有一个关于 python 类的快速问题。 以下是设置: 我有一个类作为“策划者”类,其中包含其他类的各种实例。现在这些类需要调用另一个类的方法,但我不知道该怎么做。例如:

class mastermind(object): 
 
    def __init__(self): 
        self.hand = hand() 
 
    def iNeedToCallThisMethod(self, funzies): 
        print funzies 
 
class hand(object): 
 
    def __init(self): 
        pass #here should be a call to the mastermind instance's method 
 
example = mastermind() 

希望你们能帮我解决这个问题,我的脑子里冒着热气!非常感谢!

请您参考如下方法:

如果你想调用mastermind的方法,你需要有对它的引用。

例如

class mastermind(object): 
    def __init__(self): 
        self.hand = hand(self) 
 
    def iNeedToCallThisMethod(self, funzies): 
        print funzies 
 
class hand(object): 
    def __init__(self, mastermind) 
        mastermind.iNeedToCallThisMethod('funzies')