我目前正在使用OOP,我有一个问题,我们可以在课堂上创建一个课程吗?例如,一个叫做人类的人可以分为两部分(女性 - 男性)吗?每个班级(女性 - 男性)都有自己的状态和行为。
答案 0 :(得分:1)
是的,你可以这样做,但如果你把男性和女性分成不同的类并且只是从Human类继承,那会更好。
class Human:
def __init__(self, height, weight):
self.height = height
self.weight = weight
class Male(Human):
def __init__(self, name):
Human.__init__(self, height, weight) # This will inherit every attribute of the parent class.
self.name = name
class Female(Human):
...some more code