teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2018/04/28 00:34

投稿

SpecialTukimiya
SpecialTukimiya

スコア192

title CHANGED
File without changes
body CHANGED
@@ -81,7 +81,53 @@
81
81
  a = MyClass() # MyClass のインスタンスを生成
82
82
  TypeError: __init__() missing 2 required positional arguments: 'Coca_Cola' and 'Cider'
83
83
 
84
+ ```
84
85
 
85
86
 
87
+ #完成したコード
88
+ ```python
89
+ class MyClass:
90
+
91
+ def __init__(self,Coca_Cola,Cider): #コンストラクタ
92
+ # 渡された値を代入
93
+ self.Coca_Cola=Coca_Cola
94
+ self.Cider=Cider
95
+
96
+ def __str__(self):
97
+ return "name:{0._Coca_Cola}, {0._Cider}".format(self)
98
+
99
+
100
+
101
+
102
+ # メンバ関数を定義
103
+ def set_value(self,tes): # クラス内の メソッド
104
+ self.Noo = tes #メンバ変数に代入
105
+
106
+ # 表示
107
+ def value_prints(self):
108
+ print(self.Noo)
109
+
110
+ def Add(self,a,b):
111
+ return a+b
86
112
 
113
+
114
+ if __name__ == "__main__":
115
+
116
+ #tes=list(map(int,input().split())) #test用
117
+ tes=[5,5]
118
+
119
+ # インスタンスからコンストラクタに渡す エラー
120
+ #MyClass=tes # 2つの必要な位置引数が欠けている
121
+ a = MyClass(5,5) # MyClass のインスタンスを生成
122
+
123
+ # 変数に代入
124
+ a.set_value(tes)
125
+
126
+ # 表示
127
+ a.value_prints()#メンバ関数
128
+
129
+ # 計算
130
+ print(a.Add(tes[0],tes[1]))#メンバ関数
131
+
132
+
87
133
  ```