我试图让Child类调用并使用Parent的__init__method,除了在Child中使用super()方法之外,还有其他一些方法可以做到这一点吗?我被告知要避免使用super()是可能的,因此我想知道。除了super()之外,还有其他一些方法可以调用Parent的__init__方法吗?
# -*- coding: utf-8 -*-
class Room(object):
def __init__(self, current_room):
self.current_room = current_room
print("You are now in Room #{}".format(self.current_room))
class EmptyStartRoom(Room):
def __init__(self, current_room=1):
super().__init__(current_room)
class ChestRoomKey1(Room):
def __init__(self, current_room=2):
super().__init__(current_room)
a_room = EmptyStartRoom()
other_room = ChestRoomKey1()
从上面的代码中,我得到:
您现在在房间#1
您现在在房间#2
为什么要避免使用'super()'? – Blender
@Blender如果我不确定自己在做什么,我应该避免使用它,如果可能的话,我会被告知有使用super()的商品和不便之处,所以作为初学者。也许情况并非如此? –