2017-11-20 9 views
-1

私は2つのクラスAとBを持っていますが、クラスBのクラスAからメソッドを実行したいのですがコードは書いてありますが、エラー:Python3他のクラスのあるクラスのメソッドを呼び出す方法

AttributeError: 'B' object has no attribute '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 

ランあなたはクラスAをインスタンス化する必要があるプログラム

b = B() 
b.b1() 
+0

試してみてください。 'F = A()TEST_1()' –

+0

@MauriceMeyerは、エラーが存在する:TypeError例外:TEST_1(1つの)必要な位置引数が欠落: '自己' – Kaker

+0

あなたの実際の質問は何ですか?なぜあなたは 'AttributeError'を取得しているのか理解していますか? – Goyo

答えて

1

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 
+0

ありがとうalot working good – Kaker

関連する問題