Python 编程 | 连载 16 - 类的特性

141 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天,点击查看活动详情

一、类的继承性

什么是继承:

  • 通过继承来获得所继承的类的功能
  • 被继承的类成为父类,继承类成为子类
  • 可以提高代码的重用率

父类与子类的关系:

  • 子类拥有父类的所有属性和方法
  • 父类不具备子类的独有的属性和方法
  • 定义子类时,将父类传入子类的参数内
  • 子类实例化可以调用自己与父类的函数和属性
class Human():

    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

    def breath(self):
        print('{}可以呼吸'.format(self.name))


class Student(Human):

    def study(self):
        print('{}可以学习'.format(self.name))


class Teacher(Human):

    def teach(self):
        print('{}可以教学'.format(self.name))

human = Human('夸父', '男')
human.breath()

teacher = Teacher('孔子', '男')
teacher.teach()

student = Student('子渊', '男')
student.study()

image.png

子类可以调用父类的方法,各个子类之间的方法式独立的,父类不能调用子类的方法

super 关键字

Python 中子类继承父类的方法而使用的关键字,当子类继承父类后,就可以通过 super 调用父类的方法,无须传递 self 参数

class Human():

    def __init__(self):
        print('I am Human')

class Student(Human):

    def __init__(self):
        print('I am Student')
        # 调用父类的构造方法
        super().__init__()

if __name__ == '__main__':
    stu = Student()

image.png

super 函数传值,直接在调用时传入自定义的参数即可,self 无须传递

class Human():

    def __init__(self, name):
        self.name = name
        print('I am Human, My name is {}'.format(name))

class Student(Human):

    def __init__(self):
        print('I am Student')
        super().__init__('女娲')

if __name__ == '__main__':
    stu = Student()

image.png

类的多重继承

类的多重继承既一个类可以同时继承多个父类,其他语言如 Java 中只能同时继承一个类。Python 中实现多重继承只需要将被继承的类放入子类的参数位中,使用逗号隔开,继承的顺序是从左向右依次继承

class Human():

    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

    def breath(self):
        print('{}可以呼吸'.format(self.name))

class Teacher():

    def __init__(self, name):
        self.name = name

    def teach(self):
        print('{}可以教学'.format(self.name))

class Student(Teacher, Human):

    def study(self):
        print('{}可以学习'.format(self.name))

# 使用Human父类的构造函数实例化
student = Student('子渊', '男')

student.study()
student.breath()
student.teach()

image.png

Student 类继承了 Human 类和 Teacher 类,Student 类的实例化对象可以调用 Human 和 Teacher 的类方法,但是当两个父类中存在同名的函数或者构造方法时,优先使用继承的第一个父类的函数

# 上面代码不变
student_01 = Student('孟子')
student_01.teach()
student_01.breath()

这里使用 Teacher 类的构造函数进行实例化对象

image.png

错误提示是缺少 gender 参数,这是 Human 类的构造方法,因此 Teacher 类的构造方法被覆盖了

class Human():

    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

    def breath(self):
        print('{}可以呼吸'.format(self.name))

class Teacher():

    def __init__(self, name):
        self.name = name

    def breath(self):
        print('Teacher也可以呼吸')

    def teach(self):
        print('{}可以教学'.format(self.name))

class Student(Teacher, Human):

    def study(self):
        print('{}可以学习'.format(self.name))

student_01 = Student('孟子')
student_01.teach()
student_01.breath()

在 Teacher 类中也增加一个breath方法,并且将Student继承时将Teacher放在前面、

image.png

根据打印结果,Human类的构造方法和breath方法都被覆盖了。

__mor__ 函数将显示类的继承顺序。

print(Student.__mro__)

image.png

Student 类先继承的 Teacher 类,再继承的 Human 类,最后继承了基类 object

二、类的多态性

类的多态既同一个功能或函数多状态化,在子类中重写父类的方法即可实现多态

class Human():

    def breath(self):
        print('Human can breath')

class Student(Human):

    def breath(self):
        print('Student can breath')

if __name__ == '__main__':
    stu = Student()
    stu.breath()
    

image.png

在子类中定义同名函数,即可重写父类中的函数,并实现与父类不同的功能