我試圖讓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()的商品和不便之處,所以作爲初學者。也許情況並非如此? –