面向对象三大特性: 封装 继承 # 动物包含所有的动物,动物又包含会飞的不会飞的,飞禽走兽等 # 不同的动物都能吃喝拉撒睡 # 大类已经包含吃喝拉撒,小类就不需要包含了 多态 class People: def __init__(self,name,age): self.name = name # 等号两边的 name 变量名可以不一致,比如 self.NAme = name; name 是(self,name,age)中的参数名,NAme 是赋的变量名 self.age = age def eat(self): print("%s is eating..."%self.name) def talk(self): print("%s is talking..."%self.name) def sleep(self): print("%s is sleeping..."%self.name) # 现在需要写一个子类,子类是男人 # Man 需要继承父类 # 继承后,里面什么也不写也没关系 class Man(People): pass # 实例化 m1 = Man() ---> TypeError: __init__() missing 2 required positional arguments: 'name' and 'age' 需要传名字才可以,因为是继承了的
1-1 class People: def __init__(self,name,age): self.name = name # 等号两边的 name 变量名可以不一致,比如 self.NAme = name; name 是(self,name,age)中的参数名,NAme 是赋的变量名 self.age = age def eat(self): print("%s is eating..."%self.name) def talk(self): print("%s is talking..."%self.name) def sleep(self): print("%s is sleeping..."%self.name) # 现在需要写一个子类,子类是男人 # Man 需要继承父类 # 继承后,里面什么也不写也没关系 class Man(People): pass # 实例化 m1 = Man("NiuHanYang",222) # 调用 m1.eat() ---> NiuHanYang is eating... Man直接继承了父类的方法
1-2 可以写一些自己的方法吗? class People: def __init__(self,name,age): self.name = name # 等号两边的 name 变量名可以不一致,比如 self.NAme = name; name 是(self,name,age)中的参数名,NAme 是赋的变量名 self.age = age def eat(self): print("%s is eating..."%self.name) def talk(self): print("%s is talking..."%self.name) def sleep(self): print("%s is sleeping..."%self.name) # 现在需要写一个子类,子类是男人 # Man 需要继承父类 # 继承后,里面什么也不写也没关系 class Man(People): def piao(self): print("%s is piaoing.....20s......done."%self.name) m1 = Man("NiuHanYang",222) m1.eat() m1.piao() ---> NiuHanYang is eating... NiuHanYang is piaoing.....20s......done.
1-3-1 可以重构父类方法吗?比如增加新功能 class People: def __init__(self,name,age): self.name = name # 等号两边的 name 变量名可以不一致,比如 self.NAme = name; name 是(self,name,age)中的参数名,NAme 是赋的变量名 self.age = age def eat(self): print("%s is eating..."%self.name) def talk(self): print("%s is talking..."%self.name) def sleep(self): print("%s is sleeping..."%self.name) class Man(People): def piao(self): print("%s is piaoing.....20s......done."%self.name) def sleep(self): print("man is sleeping") m1 = Man("NiuHanYang",222) m1.eat() m1.piao() m1.sleep() ---> NiuHanYang is eating... NiuHanYang is piaoing.....20s......done. man is sleeping 父类的没有执行,被覆盖了
1-3-2 但是,这里想要的是增加新功能 class People: def __init__(self,name,age): self.name = name # 等号两边的 name 变量名可以不一致,比如 self.NAme = name; name 是(self,name,age)中的参数名,NAme 是赋的变量名 self.age = age def eat(self): print("%s is eating..."%self.name) def talk(self): print("%s is talking..."%self.name) def sleep(self): print("%s is sleeping..."%self.name) class Man(People): def piao(self): print("%s is piaoing.....20s......done."%self.name) def sleep(self): People.sleep(self) print("man is sleeping") m1 = Man("NiuHanYang",222) m1.eat() m1.piao() m1.sleep() ---> NiuHanYang is eating... NiuHanYang is piaoing.....20s......done. NiuHanYang is sleeping... man is sleeping 直接调用父类方法,然后将self自己传进去,父类方法就执行了 这就等于重构了父类方法
1-4-1 再写一个类 class People: def __init__(self,name,age): self.name = name # 等号两边的 name 变量名可以不一致,比如 self.NAme = name; name 是(self,name,age)中的参数名,NAme 是赋的变量名 self.age = age def eat(self): print("%s is eating..."%self.name) def talk(self): print("%s is talking..."%self.name) def sleep(self): print("%s is sleeping..."%self.name) class Man(People): def piao(self): print("%s is piaoing.....20s......done."%self.name) def sleep(self): People.sleep(self) print("man is sleeping") class Woman(People): def get_birth(self): print("%s is born a baby...."%self.name) m1 = Man("NiuHanYang",222) m1.eat() m1.piao() m1.sleep() w1 = Woman("ChenRonghua",26) w1.get_birth() ---> NiuHanYang is eating... NiuHanYang is piaoing.....20s......done. NiuHanYang is sleeping... man is sleeping ChenRonghua is born a baby....
1-4-2 Man 和 Woman 都 继承了 People 既然都属于同一个父类,Woman 可以调用 Man 中的 piao 吗? class People: def __init__(self,name,age): self.name = name # 等号两边的 name 变量名可以不一致,比如 self.NAme = name; name 是(self,name,age)中的参数名,NAme 是赋的变量名 self.age = age def eat(self): print("%s is eating..."%self.name) def talk(self): print("%s is talking..."%self.name) def sleep(self): print("%s is sleeping..."%self.name) class Man(People): def piao(self): print("%s is piaoing.....20s......done."%self.name) def sleep(self): People.sleep(self) print("man is sleeping") class Woman(People): def get_birth(self): print("%s is born a baby...."%self.name) m1 = Man("NiuHanYang",222) m1.eat() m1.piao() m1.sleep() w1 = Woman("ChenRonghua",26) w1.get_birth() w1.piao() ---> AttributeError: 'Woman' object has no attribute 'piao' 男女不同的地方,所以才弄出两个子类,所以 Woman 是不可以调用 Man 的 就是因为有不同,所以才分别继承了父类