我正在尝试围绕类继承。我想要一个police_officer
班和一个police_chief
班。我希望我的酋长拥有特殊的权力,同时也拥有与他的军官相同的权力。以下是我到目前为止的情况:
class police_officer(object):
def __init__(self, name):
self.name = name
def arrest(self, criminal):
print(criminal + ' is under arrest!')
jail.append(criminal)
class police_chief(police_officer):
def fire_officer(self, officer):
print(officer.name + ' was fired!')
officers.remove(officer)
jail = []
officers = []
suspects = ['Joseph', 'Charles', 'Eric']
officers.append(police_officer('Edward'))
officers.append(police_officer('Jerry'))
officers.append(police_officer('Robert'))
chief = police_chief(officers[0])
officers[1].arrest(suspects[2])
chief.fire_officer(officers[2])
这似乎按照我希望的方式运作,但是现在我想给我的主要班级一个def retire(self, new_chief)
职能,他可以在那里退休并让现有的一名官员担任主任,不是police_chief
的全新实例。如果我能找到一个方法让酋长再次成为一名正式军官,同时又选择下一任酋长应该是谁,那就更好了。我希望我对自己要做的事情已经足够清楚了,我仍然是Python的新手。谢谢!