Python 如何在两个类之间调用方法?
我有一个类a。在一个实例的_uinit _;方法期间 我创建了以下两个类B和C的实例: 一旦全部设置好,我需要在B的方法中调用C的方法 例如: 触发:Python 如何在两个类之间调用方法?,python,python-2.7,python-2.x,Python,Python 2.7,Python 2.x,我有一个类a。在一个实例的_uinit _;方法期间 我创建了以下两个类B和C的实例: 一旦全部设置好,我需要在B的方法中调用C的方法 例如: 触发: b.call_c() 是否: 要实现此结构,我需要做什么?您需要将self或c传递给B,这样它就可以了解另一个对象。您需要将self或c传递给B,这样它就可以了解另一个对象 我需要在B的方法中调用C的方法 基本上,如果方法不是类方法或静态方法,那么调用方法始终意味着您可以访问c类的c对象 请看下面的示例: #!python3 class B:
b.call_c()
是否:
要实现此结构,我需要做什么?您需要将self或c传递给B,这样它就可以了解另一个对象。您需要将self或c传递给B,这样它就可以了解另一个对象 我需要在B的方法中调用C的方法 基本上,如果方法不是类方法或静态方法,那么调用方法始终意味着您可以访问c类的c对象 请看下面的示例:
#!python3
class B:
def __init__(self, value):
self.value = value
def __str__(self):
return 'class B object with the value ' + str(self.value)
class C:
def __init__(self, value):
self.value = value
def __str__(self):
return 'class C object with the value ' + str(self.value)
class A:
def __init__(self, value):
self.value = value
self.b = B(value * 2)
self.c = C(value * 3)
def __str__(self):
lst = ['class A object with the value ' + str(self.value),
' containing the ' + self.b.__str__(),
' containing also the ' + str(self.c),
]
return '\n'.join(lst)
a = A(1)
print(a)
print(a.b)
print(a.c)
self.b._str__是从A类对象的方法调用b类对象的方法的示例。strself.c是相同的,仅通过str函数间接调用
将显示以下内容:
class A object with the value 1
containing the class B object with the value 2
containing also the class C object with the value 3
class B object with the value 2
class C object with the value 3
我需要在B的方法中调用C的方法
基本上,如果方法不是类方法或静态方法,那么调用方法始终意味着您可以访问c类的c对象
请看下面的示例:
#!python3
class B:
def __init__(self, value):
self.value = value
def __str__(self):
return 'class B object with the value ' + str(self.value)
class C:
def __init__(self, value):
self.value = value
def __str__(self):
return 'class C object with the value ' + str(self.value)
class A:
def __init__(self, value):
self.value = value
self.b = B(value * 2)
self.c = C(value * 3)
def __str__(self):
lst = ['class A object with the value ' + str(self.value),
' containing the ' + self.b.__str__(),
' containing also the ' + str(self.c),
]
return '\n'.join(lst)
a = A(1)
print(a)
print(a.b)
print(a.c)
self.b._str__是从A类对象的方法调用b类对象的方法的示例。strself.c是相同的,仅通过str函数间接调用
将显示以下内容:
class A object with the value 1
containing the class B object with the value 2
containing also the class C object with the value 3
class B object with the value 2
class C object with the value 3
以下是将A对象作为父对象/容器对象传递给B和C时的情况:
class A(object):
def __init__(self):
self.b = B(self)
self.c = C(self)
class B(object):
def __init__(self, parent):
self.parent = parent
def call_c(self):
self.parent.c.a_method_of_c()
class C(object):
def __init__(self, parent):
self.parent = parent
# whatever...
或者,您可以将C实例传递给B的初始值设定项,如下所示:
class A(object):
def __init__(self):
self.c = C()
self.b = B(self.c)
class B(object):
def __init__(self, c):
self.cobj = c
def call_c(self):
self.cobj.a_method_of_c()
class C(object):
# whatever...
我更喜欢第二种方法,因为它消除了B和C对A的依赖,以及A实现B和C属性的必要性
如果B和C必须互相调用方法,您仍然可以使用A来建立这些关联,但使B和C不知道A:
class A(object):
def __init__(self):
self.b = B()
self.c = C()
self.b.cobj = self.c
self.c.bobj = self.b
class B(object):
def __init__(self, c):
self.cobj = None
def call_c(self):
if self.cobj is not None:
self.cobj.a_method_of_c()
else:
raise Exception("B instance not fully initialized")
class C(object):
# similar to B
一般来说,您的目标是尽量避免或至少最小化这些依赖关系—让家长知道孩子的情况,但让孩子不知道家长的情况。或者容器知道其包含的对象,但包含的对象不知道其容器。一旦将循环引用添加回父对象或容器对象的引用,事情就会以各种令人惊讶的方式变得糟糕。当清除其中一个链接而不是反射链接时,关系可能会损坏。或者循环关系中的垃圾收集在Python中可能会遇到棘手的问题,但如果这些对象和关系在框架中被持久化或复制,则可能无法处理。如果将a对象作为父对象/容器对象传递给B和C,则情况如下:
class A(object):
def __init__(self):
self.b = B(self)
self.c = C(self)
class B(object):
def __init__(self, parent):
self.parent = parent
def call_c(self):
self.parent.c.a_method_of_c()
class C(object):
def __init__(self, parent):
self.parent = parent
# whatever...
或者,您可以将C实例传递给B的初始值设定项,如下所示:
class A(object):
def __init__(self):
self.c = C()
self.b = B(self.c)
class B(object):
def __init__(self, c):
self.cobj = c
def call_c(self):
self.cobj.a_method_of_c()
class C(object):
# whatever...
我更喜欢第二种方法,因为它消除了B和C对A的依赖,以及A实现B和C属性的必要性
如果B和C必须互相调用方法,您仍然可以使用A来建立这些关联,但使B和C不知道A:
class A(object):
def __init__(self):
self.b = B()
self.c = C()
self.b.cobj = self.c
self.c.bobj = self.b
class B(object):
def __init__(self, c):
self.cobj = None
def call_c(self):
if self.cobj is not None:
self.cobj.a_method_of_c()
else:
raise Exception("B instance not fully initialized")
class C(object):
# similar to B
一般来说,您的目标是尽量避免或至少最小化这些依赖关系—让家长知道孩子的情况,但让孩子不知道家长的情况。或者容器知道其包含的对象,但包含的对象不知道其容器。一旦将循环引用添加回父对象或容器对象的引用,事情就会以各种令人惊讶的方式变得糟糕。当清除其中一个链接而不是反射链接时,关系可能会损坏。或者循环关系中的垃圾收集在Python中可能会遇到棘手的问题,但如果这些对象和关系在框架中被持久化或复制,则可能无法处理。Ignacio,我尝试传递父对象,然后执行parent.c,但失败了。你能说得再详细一点吗?你是什么意思,也许我弄错了?我会粘贴代码,但我已经做了一个多小时了,我尝试了很多方法来删除。如果要使对象可用于初始化器以外的方法,则需要将其存储在正在初始化的对象上。Ignacio,我尝试传递父对象,然后执行parent.c,但失败了。你能说得再详细一点吗?你是什么意思,也许我弄错了?我会粘贴代码,但我已经做了一个多小时了,我尝试了很多方法来删除。如果你想让对象可用于初始化器以外的方法,那么你需要将其存储在被初始化的对象上。没有为call_c定义名为parent的内容来执行parent.c.a_method_of_c调用。它需要类C实例的名称,这可以作为附加参数传递给它,也可以存储在它的自参数引用的类B对象的实例中。没有为call_C定义命名的parent来执行parent.C.a_方法_of_C调用。它需要名字
对于类C的实例,可以将其作为附加参数传递给它,也可以将其存储在其自参数引用的类B对象的实例中。非常感谢@PaulMcGuire提供了这个精彩的示例。太好了。非常感谢@PaulMcGuire提供的这个精彩的例子。杰出的