继承
继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。
通过继承创建的新类称为“子类”或“派生类”。
被继承的类称为“基类”、“父类”或“超类”。
继承的过程,就是从一般到特殊的过程。
要实现继承,可以通过“继承”(Inheritance)和“组合”(Composition)来实现。
一般情况下,一个子类只能有一个基类,要实现多重继承,可以通过多级继承来实现。
class People: def __init__(self,name,age): self.name = 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",22) m1.eat() ---->NiuHanYang is eating... m1.piao()----->NiuHanYang is piaoing ..... 20s....done. m1.sleep() ----->NiuHanYang is sleeping...(先调用了父类方法) ------>man is sleeping w1 = Woman("ChenRonghua",26) w1.piao()----->xx
class People(object): #新式类 #class People: def __init__(self,name,age): self.name = 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): #重构构造函数 name age 必须有 因为重构覆盖父类 要重写属性 并加上自己属性 def __init__(self,name,age,money): People.__init__(self,name,age) #super(Man,self).__init__(name,age) #新式类写法调用父类,在多继承时常用 节省代码(People.__init__(self,name,age) //Animals.__init__(self,name,age) ) self.money = money print("%s 一出生就有%s money" %(self.name,self.money)) #m1 = Man("NiuHanYang",22,1000) #一初始化 有对象 就调用_init_(),---->NiuHanYang一出生就有1000 money" #子类自身方法 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",22) m1.eat() ---->NiuHanYang is eating... m1.piao()----->NiuHanYang is piaoing ..... 20s....done. m1.sleep() ----->NiuHanYang is sleeping...(先调用了父类方法) ------>man is sleeping w1 = Woman("ChenRonghua",26) w1.piao()----->xx w1.name = "陈三炮"
多继承
python支持多继承 就是有多个父类
# class People: 经典类 class People(object): #新式类 def __init__(self,name,age): self.name = name self.age = age self.friends = [] print("--doens't run ") 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 Relation(object): #即使不写初始化 也可以 因为People先已经初始化了name age #即使Man(People,Relation) 也可以 因为Man本身初始化了name age #Man(People,Relation) 从左到右执行 若man无init找people init... def make_friends(self,obj): #w1 print("%s is making friends with %s" % (self.name,obj.name)) self.friends.append(obj.name) class Man(Relation,People): def __init__(self,name,age,money): super(Man,self).__init__(name,age) #新式类写法 self.money = money print("%s 一出生就有%s money" %(self.name,self.money)) def piao(self): print("%s is piaoing ..... 20s....done." % self.name) def sleep(self): People.sleep(self) print("man is sleeping ") class Woman(People,Relation): def get_birth(self): print("%s is born a baby...." % self.name) m1 = Man("NiuHanYang",22) w1 = Woman("ChenRonghua",26) m1.make_friends(w1)---->NiuHanYangis making friends with ChenRonghua
新式类与经典类的继承顺序:
class A: def __init__(self): print("A") class B(A): pass # def __init__(self): # print("B") class C(A): pass # def __init__(self): # print("C") class D(B,C): pass # def __init__(self): # print("D") obj = D()
以构造函数为例:
广度优先:如果 D 无_init_找 B D---->B
如果 B 无_init_找 C D---->B------>C
如果 C 无_init_找 A D---->B------->C------>A
广度优先:如果 D 无_init_找 B D---->B
如果 B 无_init_找 A D---->B------>A
如果 A 无_init_找 C D---->B------->A------>C
py2 经典类是按深度优先来继承的,新式类是按广度优先来继承的
py3 经典类和新式类都是统一按广度优先来继承的
继承学校例子:https://www.cnblogs.com/alex3714/articles/5188179.html
多态:
一种接口 多种实现
多态性(polymorphisn)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。
多态的作用:
我们知道,封装可以隐藏实现细节,使得代码模块化;继承可以扩展已存在的代码模块(类);它们的目的都是为了——代码重用。而多态则是为了实现另一个目的——接口重用!多态的作用,就是为了类在继承和派生的时候,保证使用“家谱”中任一类的实例的某一属性时的正确调用。
Pyhon 很多语法都是支持多态的,比如 len(),sorted(), 你给len传字符串就返回字符串的长度,传列表就返回列表长度。
但是没有直接语法支持多态(不同于java)
class Animal(object): def __init__(self, name): self.name = name def talk(self): pass class Cat(Animal): def talk(self): print('%s: 喵喵喵!' %self.name) class Dog(Animal): def talk(self): print('%s: 汪!汪!汪!' %self.name) def func(obj): #一个接口,多种形态 obj.talk() c1 = Cat('小晴') c1.talk() ---->小晴喵喵喵! d1 = Dog('李磊') d1=talk() ---->李磊喵喵喵! func(c1) ---->小晴喵喵喵! func(d1) ---->李磊喵喵喵!