类与面向对象的问题
class Saiyan:born_place = '贝吉塔行星'
character = '天生好战性格暴躁'
def __init__(self,name):
self.name = name
def transform(self):
print('变身巨猿')
def __str__(self):
return '我是来自贝吉塔行星的卡卡罗特'
kakarotto = Saiyan('卡卡罗特')
self.transform()
print(kakarotto)
NameError: name 'self' is not defined on line 15
为什么报错 怎么解决 本帖最后由 Twilight6 于 2020-8-16 22:25 编辑
把self.transform() 改成 kakarotto.transform()
你 kakarotto = Saiyan('卡卡罗特') 实例化对象时候,self 就代表 kakarotto 了
class Saiyan:
born_place = '贝吉塔行星'
character = '天生好战性格暴躁'
def __init__(self, name):
self.name = name
def transform(self):
print('变身巨猿')
def __str__(self):
return '我是来自贝吉塔行星的卡卡罗特'
kakarotto = Saiyan('卡卡罗特')
kakarotto.transform()
print(kakarotto) Twilight6 发表于 2020-8-16 22:24
把self.transform() 改成 kakarotto.transform()
你 kakarotto = Saiyan('卡卡罗特') 实例化对象时 ...
嗷嗷...知道了{:10_254:}
页:
[1]