我有两个类 A 和 B,我想在类 B 中运行类 A 中的方法。我编写了代码但它不起作用,我收到以下错误:
AttributeError: 'B' 对象没有属性 'testPrint'
我的课程:
class A:
def __init__(self):
self.v = 'A'
def test_1(self):
i = 1
print('Function test_1 in class A: ')
x = self.testPrint(i) # i think error is here
return x
def testPrint(self, i):
return 'testPrint: '+i
class B:
def __init__(self):
self.v = 'B'
def b1(self):
print('wywolanie funkcji z klasy b')
f = A.test_1(self)
return f
运行程序
b = B()
b.b1()
您需要实例化 A 类:
class A:
def __init__(self):
self.v = 'A'
def test_1(self):
i = 1
print('Function test_1 in class A: ')
x = self.testPrint(i) # i think error is here
return x
def testPrint(self, i):
return 'testPrint: %s' % i
class B:
def __init__(self):
self.v = 'B'
def b1(self):
print('wywolanie funkcji z klasy b')
f = A().test_1()
return f
b = B()
res = b.b1()
print (res)
返回(Python3):
wywolanie funkcji z klasy b
Function test_1 in class A:
testPrint:1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句