私はoopの概念を学びたいと思っています。私はPythonを選択しました。私が知る限り、self.countとemployee.countはどちらもクラス変数countを呼び出していて、両方とも同じ値を持つ必要があります。ここpythonのself.variable nameとclassname.variableの違い
self.count = self.count + 1
self.count
:ただし、以下のコードのために、私はself.countが1であり、employee.countは0
class employee:
count=0
def __init__(self,x):
self.x=x
self.count=self.count+1
print ("this method is executed")
print (self.count)
print (employee.count)
emp1=employee("John")
彼らは明らかに同じ変数ではありません。あなたの質問は何ですか?他の '従業員 'オブジェクトを作ろうとしましたか? –
いいえ... self.countとemployee.countは、前に宣言した同じクラス変数の数を参照します。 – vijay
@vijayしかし、インスタンス変数 'self.count'を宣言するまで... – Keiwan