来源:python中国网 时间:2019-06-06

  继承是一个概念而已,个人觉得继承是一种编程的方法,是人类"懒惰"的体现。

  官方点说:

  1)继承是面向对象软件技术当中的一个概念。如果一个类别A“继承自”另一个类别B,就把这个A称为“B的子类别”,而把B称为“A的父类别”也可以称“B是A的超类”。继承可以使得子类别具有父类别的各种属性和方法,而不需要再次编写相同的代码。

  2)在令子类别继承父类别的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类别的原有属性和方法,使其获得与父类别不同的功能。另外,为子类别追加新的属性和方法也是常见的做法。

  3)一般静态的面向对象编程语言,继承属于静态的,意即在子类别的行为在编译期就已经决定,无法在执行期扩充。

  继承的分类

  下面例子中:Aminal叫做父类,基类,超类。PersonPigDog:子类,派生类。继承:可以分单继承,多继承。

# ‐*‐ coding: utf‐8 ‐*‐
class Animal:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
class People(Animal):
    def walk(self):
        print("People [%s] is walking..." % self.name)
class Pig(Animal):
    def eat(self):
        print("Pig [%s] is eating..." % self.name)
class Dog(Animal):
    def eat(self):
        print("Dog [%s] is eating..." % self.name)
person = People("laowang",25,"Male")
pig = Pig("Mjj",4,"公")
dog = Dog("毛毛",3,"母")
person.walk()
pig.eat()
dog.eat()
D:installpython3python.exe D:/pyscript/test/test.py
People [laowang] is walking...
Pig [Mjj] is eating...
Dog [毛毛] is eating...
Process finished with exit code 0


   继承优点:

  1,增加了类的耦合性。

  2,减少了重复代码。

  3,使得代码更加规范化,合理化。

  继承父类的方法&重构

# ‐*‐ coding: utf‐8 ‐*‐
class Animal:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
    def eat(self):
        print("[%s] is eating..."%self.name)
class People(Animal):
    def walk(self):
        print("People [%s] is walking..." % self.name)
class Pig(Animal):
    def eat(self): # 对父类的方法进行了重构
        print("Pig [%s] is eating..." % self.name)
person = People("laowang",25,"Male")
pig = Pig("Mjj",4,"公")
person.walk()
person.eat() # 继承自父类的eat方法
pig.eat()
D:installpython3python.exe D:/pyscript/test/test.py
People [laowang] is walking...
[laowang] is eating...
Pig [Mjj] is eating...
Process finished with exit code 0

  同时执行子类&父类方法

  1)如果想执行父类的init方法,这个方法并且子类中也用,那么就在子类的方法中写上:父类.func(子类对象,其他参数)

# ‐*‐ coding: utf‐8 ‐*‐
class Animal:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
    def eat(self):
        print("[%s] is eating..."%self.name)
class People(Animal):
    def __init__(self,name,age,sex,race):
        Animal.__init__(self,name,age,sex) # 先执行父类方法
        self.race = race  # 再加上子类的属性
        print("初始化了一个人....")
    def walk(self):
        print("People [%s] is walking..." % self.name)
person = People('laowang',25,'Male','哈哈')
print(person.name)
D:installpython3python.exe D:/pyscript/test/test.py
初始化了一个人....
laowang
Process finished with exit code 0


    2)利用super,super().func(参数)

# ‐*‐ coding: utf‐8 ‐*‐
class Animal:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
    def eat(self):
        print("[%s] is eating..."%self.name)
class People(Animal):
    def __init__(self,name,age,sex,race):
        super(People,self).__init__(name,age,sex)
        self.race = race  # 再加上子类的属性
        print("初始化了一个人....")
person = People('laowang',25,'Male','哈哈')
print(person.name)
D:installpython3python.exe D:/pyscript/test/test.py
初始化了一个人....
laowang
Process finished with exit code 0


  多继承MultipleInheritance

  首先了解下新式类,新式类其实就是所有的类都继承自object,在python2中需要显式的基础,python3中则不用。

classA: # 经典类
  pass
classB(object): # 新式类
  pass

  一般而言,在python2中,经典类采用的是深度优先查找法,新式类采用的是广度优先,在python3中,无论是经典类,还是新式类,都是按广度优先查找,但是某些情况还会涉及C3算法来确定继承顺序,简单的多继承,c3算出来跟深度优先效果一样。通常很少有人把继承写的这么复杂,暂时先做了解。以下三个类大家自己研究下!

 
class ShenXian:
    """神仙类"""
    def fly(self):
        print("神仙都会飞...")
class Monkey:
    def eat_peach(self):
        print("猴子都喜欢吃桃子...")
class MonkeyKing(ShenXian,Monkey):
    def play_goden_stick(self):
        print("孙悟空玩金箍棒...")
sxz = MonkeyKing()
sxz.eat_peach()
sxz.fly()
sxz.play_goden_stick()
"""
多继承出现重名
"""
class ShenXian:
    """神仙类"""
    def fly(self):
        print("神仙都会飞...")
    def fight(self):
        print("神仙在打架...")
class Monkey:
    def eat_peach(self):
        print("猴子都喜欢吃桃子...")
    def fight(self):
        print("猴子在打架...")
class MonkeyKing(ShenXian,Monkey):
    def play_goden_stick(self):
        print("孙悟空玩金箍棒...")
sxz = MonkeyKing()
# sxz.eat_peach()
# sxz.fly()
# sxz.play_goden_stick()
sxz.fight()
"""
继承的父类又继承爷爷类,并且还重名
"""
class ShenXianBase:
    def fight(self):
        print("神仙祖宗在打架....")
class MonkeyBase:
    def fight(self):
        print("猿猴在打架....")
class ShenXian(ShenXianBase):
    """神仙类"""
    def fly(self):
        print("神仙都会飞...")
    #def fight(self):
    #    print("神仙在打架...")
class Monkey(MonkeyBase):
    def eat_peach(self):
        print("猴子都喜欢吃桃子...")
    def fight(self):
        print("猴子在打架...")
class MonkeyKing(ShenXian,Monkey):
    def play_goden_stick(self):
        print("孙悟空玩金箍棒...")
sxz = MonkeyKing()
sxz.fight()
"""
若把ShenXian类里的fight注释掉,你说sxz.fight()会打印哪个? 是引用Monkey里的fight呢,还是ShenXianBase里的fight呢?
"""