名称 self 未定义,但我认为是?

Name self is not defined, but I thought it was?

我收到错误“NameError:名称‘_Activity__self’未定义”。我使用 self 作为所有内容和成员的参数,所以我不确定我遗漏了什么。 -- 我试图让 Activity 成为父 class 并且 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

您正在使用 __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
如果 __self 在元组中,

__self 将起作用,例如 (__self, d, t)。请注意,“init”元组中的第一个参数决定了 class 的 'self' 名称。惯例推荐“self”,但实际上它可以是任何你想要的。重要的是参数中的位置。

https://docs.python.org/3/faq/programming.html?highlight=self#what-is-self

中查看 'What is self'