我原本要介紹的是多型的概念,不過Python比較特別,可以將object直接強制性轉...如果是在強類型的Java是要做個處理的,不過多型的概念是一樣的,一不小心就性轉了
class human:
def __init__(self, gender):
self.gender =gender
def getGender(self):
print(self.gender)
class boy(human):
def __init__(self, gender):
self.gender = gender
class girl(human):
def __init__(self, gender):
self.gender = gender
guy = boy("M")
guy.getGender()
guy = girl("F")
guy.getGender()