自分で独学で勉強しているのですが継承ができずエラーになってしまします。
1週間目なのでもしコメントの考え方など違っていたら教えてください。
以下エラー内容
python
1--------------------------------------------------------------------------- 2TypeError Traceback (most recent call last) 3<ipython-input-26-7bda6b01d5ff> in <module>() 4 30 5 31 only_one_inheritance = Class_2_Inheritance # 継承先のクラス「だけ」をインスタンス化 6---> 32 only_one_inheritance.hello() 7 33 only_one_inheritance.world() 8 9TypeError: hello() missing 1 required positional argument: 'self'
class ClassDesignDrawing: # クラスという def __init__(self,name): # コンストラクタという self.name = name # self.nameはインスタンス変数。「インスタンス.変数名」で表示可能 def print_name(self): # クラスが持つ関数は メソッド print('この設計図の車メーカーは{}です'.format(self.name)) def hello(self): print ("Hello") KG = 2580 # KGはクラス変数という「クラス名.変数名」で表し全インスタンスで共通変数 CM = 1748 # KGはクラス変数という「クラス名.変数名」で表し全インスタンスで共通変数 class Class_2_Inheritance(ClassDesignDrawing):# クラスは継承(Inheritance)が可能 def __init__(self,name): # コンストラクタという self.name = name # self.nameはインスタンス変数。「インスタンス.変数名」で表示可能 def world(self): print ("World") afterproduction = ClassDesignDrawing('トヨタ') # インスタンス化(実体化)という afterproduction.print_name() # print_name()メソッドをコールする car_explain = "車体総重量は{}KGで長さは{}CMです。"#変数は{}で指定 print(car_explain.format(afterproduction.KG, afterproduction.CM))#format () に入れる引数は変数いずれも可能。 afterproduction.newname_brand = "lexus" # インスタンス変数は新しく追加できる ClassDesignDrawing.CM = 3456 # クラス変数は変更できる ClassDesignDrawing.COLOR = "BLUE" # クラス変数は追加もできる only_one_inheritance = Class_2_Inheritance() # 継承先のクラス「だけ」をインスタンス化 only_one_inheritance.hello() only_one_inheritance.world()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/07/20 04:08
2019/07/20 04:18 編集
退会済みユーザー
2019/07/20 04:25