質問編集履歴
3
あ
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,15 +1,3 @@
|
|
1
|
-
多重継承のチェックでエラーが出てしまいます。
|
2
|
-
|
3
|
-
class ClassDesignDrawing:
|
4
|
-
|
5
|
-
class Class_2_Inheritance(ClassDesignDrawing)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
よろしくお願いします
|
10
|
-
|
11
|
-
|
12
|
-
|
13
1
|
#エラー内容
|
14
2
|
|
15
3
|
|
@@ -39,38 +27,6 @@
|
|
39
27
|
order (MRO) for bases ClassDesignDrawing, Class_2_Inheritance
|
40
28
|
|
41
29
|
```
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
```ここに言語を入力
|
50
|
-
|
51
|
-
class ClassDesignDrawing: # クラスという 継承元の場合は親クラスという
|
52
|
-
|
53
|
-
def __init__(self,name): # コンストラクタという
|
54
|
-
|
55
|
-
self.name = name # self.nameはインスタンス変数。「インスタンス.変数名」で表示可能
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
def print_name(self): # クラスが持つ関数は メソッド
|
60
|
-
|
61
|
-
print('この設計図の車メーカーは{}です'.format(self.name))
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
def hello(self):
|
66
|
-
|
67
|
-
print ("Hello")
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
KG = 2580 # KGはクラス変数という「クラス名.変数名」で表し全インスタンスで共通変数
|
72
|
-
|
73
|
-
CM = 1748 # KGはクラス変数という「クラス名.変数名」で表し全インスタンスで共通変数
|
74
30
|
|
75
31
|
|
76
32
|
|
@@ -110,40 +66,4 @@
|
|
110
66
|
|
111
67
|
|
112
68
|
|
113
|
-
|
114
|
-
|
115
|
-
afterproduction = ClassDesignDrawing('トヨタ') # インスタンス化(実体化)という
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
afterproduction.print_name() # print_name()メソッドをコールする
|
120
|
-
|
121
|
-
car_explain = "車体総重量は{}KGで長さは{}CMです。"#変数は{}で指定
|
122
|
-
|
123
|
-
print(car_explain.format(afterproduction.KG, afterproduction.CM))#format () に入れる引数は変数いずれも可能。
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
afterproduction.newname_brand = "lexus" # インスタンス変数は新しく追加できる
|
128
|
-
|
129
|
-
ClassDesignDrawing.CM = 3456 # クラス変数は変更できる
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
only_one_inheritance = Class_2_Inheritance('ニッサン') # 継承先の”クラス2だけ”をインスタンス化しても
|
134
|
-
|
135
|
-
only_one_inheritance.print_name() # 継承されているから元々のクラスのprint_name()メソッドもコールできる
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
afterproduction.hello() # 親クラスのhello()メソッドをコールしたからHelloが1回表示される
|
140
|
-
|
141
|
-
only_one_inheritance.world() # もちろんクラス2のworld()メソッドもコールできる
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
only_one_inheritance.hello() # 継承されている為サブクラスのhello()メソッドがコールされHELLOが2回憑依j
|
146
|
-
|
147
|
-
|
148
|
-
|
149
69
|
```
|
2
34
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,22 +3,6 @@
|
|
3
3
|
class ClassDesignDrawing:
|
4
4
|
|
5
5
|
class Class_2_Inheritance(ClassDesignDrawing)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
という2つのクラスに対して以下のようにMyClassCという多重継承をさせたいです。
|
10
|
-
|
11
|
-
class MyClassC(ClassDesignDrawing('TATA'), Class_2_Inheritance('SUBARU')):
|
12
|
-
|
13
|
-
pass
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
a = MyClassC()
|
18
|
-
|
19
|
-
a.hello() # ClassDesignDrawing のメソッドも
|
20
|
-
|
21
|
-
a.world() # Class_2_Inheritance のメソッドも使用できる
|
22
6
|
|
23
7
|
|
24
8
|
|
1
ついき
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,21 +36,23 @@
|
|
36
36
|
|
37
37
|
TypeError Traceback (most recent call last)
|
38
38
|
|
39
|
-
<ipython-input-5
|
39
|
+
<ipython-input-5-b4a7c5c0a34c> in <module>()
|
40
40
|
|
41
|
-
3
|
41
|
+
23
|
42
42
|
|
43
|
-
|
43
|
+
24
|
44
44
|
|
45
|
-
--->
|
45
|
+
---> 25 class Class_3(ClassDesignDrawing, Class_2_Inheritance):
|
46
46
|
|
47
|
-
|
47
|
+
26 pass
|
48
48
|
|
49
|
-
|
49
|
+
27
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
-
TypeError:
|
53
|
+
TypeError: Cannot create a consistent method resolution
|
54
|
+
|
55
|
+
order (MRO) for bases ClassDesignDrawing, Class_2_Inheritance
|
54
56
|
|
55
57
|
```
|
56
58
|
|
@@ -80,7 +82,7 @@
|
|
80
82
|
|
81
83
|
print ("Hello")
|
82
84
|
|
83
|
-
|
85
|
+
|
84
86
|
|
85
87
|
KG = 2580 # KGはクラス変数という「クラス名.変数名」で表し全インスタンスで共通変数
|
86
88
|
|
@@ -94,45 +96,23 @@
|
|
94
96
|
|
95
97
|
self.name = name # self.nameはインスタンス変数。「インスタンス.変数名」で表示可能
|
96
98
|
|
97
|
-
|
99
|
+
|
98
100
|
|
99
101
|
def world(self):
|
100
102
|
|
101
103
|
print ("World")
|
102
104
|
|
103
|
-
|
105
|
+
|
104
106
|
|
105
107
|
def hello(self): # サブクラスは親クラスのhello()メソッドをオーバーライドする事ができる
|
106
108
|
|
107
109
|
print("HELLO HELLO これサブクラス書換え!")# class ClassDesignDrawingの hello(self)をオーバーライドしている
|
108
110
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
#class MyClassC(ClassDesignDrawing, Class_2_Inheritance):
|
114
111
|
|
115
112
|
|
116
113
|
|
117
114
|
|
118
|
-
|
119
|
-
class MyClassA:
|
120
|
-
|
121
|
-
def funcA(self):
|
122
|
-
|
123
|
-
print ("MyClassA:funcA")
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
class MyClassB:
|
128
|
-
|
129
|
-
def funcB(self):
|
130
|
-
|
131
|
-
print ("MyClassB:funcB")
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
class
|
115
|
+
class Class_3(ClassDesignDrawing, Class_2_Inheritance):
|
136
116
|
|
137
117
|
pass
|
138
118
|
|
@@ -172,15 +152,13 @@
|
|
172
152
|
|
173
153
|
|
174
154
|
|
175
|
-
afterproduction.hello() # 親クラスのhello()メソッドをコールしたからHello
|
155
|
+
afterproduction.hello() # 親クラスのhello()メソッドをコールしたからHelloが1回表示される
|
176
156
|
|
177
157
|
only_one_inheritance.world() # もちろんクラス2のworld()メソッドもコールできる
|
178
158
|
|
179
159
|
|
180
160
|
|
181
|
-
only_one_inheritance.hello() # 継承されている
|
161
|
+
only_one_inheritance.hello() # 継承されている為サブクラスのhello()メソッドがコールされHELLOが2回憑依j
|
182
|
-
|
183
|
-
|
184
162
|
|
185
163
|
|
186
164
|
|