我收到錯誤“NameError: name '_Activity__self' is not defined”。我在所有事情和成員中都使用 self 作為引數,所以我不確定我錯過了什么。-- 我試圖讓 Activity 成為父類,而 WakeUp 是從 Activity 派生的。
class Activity:
def __init__ (self, d, t):
__self.date = d
__self.time = t
def getDate(self):
return self.date
def getTime(self):
return self.time
class WakeUp (Activity):
def __init__ (self, d, t):
Activity.__init__(self, d, t)
__self.activityType = "WakeUp"
def getType(self):
return self.activityType
uj5u.com熱心網友回復:
您正在使用__self而不是self.
這應該有效
。
class Activity:
def __init__ (self, d, t):
self.date = d
self.time = t
def getDate(self):
return self.date
def getTime(self):
return self.time
class WakeUp (Activity):
def __init__ (self, d, t):
Activity.__init__(self, d, t)
self.activityType = "WakeUp"
def getType(self):
return self.activityType
uj5u.com熱心網友回復:
__self如果__self在元組中會起作用,例如(__self, d, t). 請注意,“ init ”元組中的第一個引數確定類的“self”名稱。約定推薦“自我”,但實際上它可以是您想要的任何東西。引數中的位置很重要。
在https://docs.python.org/3/faq/programming.html?highlight=self#what-is-self 中查看“什么是自我”
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368714.html
上一篇:Python類按子類更新父變數
