1 回答
TA贡献1934条经验 获得超2个赞
在next_scene中,您正在返回,但ClassRoom不是. 为此,您需要,然后有一个ClassRoomreturn nxt()self
class Map(object):
Senario = {'finished':Finished, 'classroom':ClassRoom,'sportclub':SportsClub, 'scienceclub':ScienceClub, 'canteen':Canteen, 'auditorium':Auditorium}
def __init__(self,start_scene):
self.start_scene = start_scene
def begin(self):
return self.next_scene(self.start_scene)
def next_scene(self,scene_name):
# adding the parentheses here will call the class
# creating an instance
nxt = self.Senario.get(scene_name)()
return nxt
举个小例子:
class X:
def something(self):
return 4
X.something()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: something() missing 1 required positional argument: 'self'
您还没有创建任何实例,因此self还不存在。但是,如果我们将其更改为:
x = X()
x.something()
4
Aself被创建是因为底层的类机制将调用 的基本实现__init__,而不涉及血淋淋的细节
添加回答
举报