首页 > 编程语言 >什么是 Python 类的继承和多继承?

什么是 Python 类的继承和多继承?

时间:2023-03-13 19:33:01浏览次数:54  
标签:__ name Person 继承 什么 self Python 父类 age

本文首发自「慕课网」,想了解更多IT干货内容,程序员圈内热闻,欢迎关注!

作者| 慕课网精英讲师 朱广蔚

在面向对象的程序设计中,定义一个新的 class 的时候,可以从某个现有的 class 继承,新的 class 称为子类,而被继承的 class 称为基类、父类或超类。

Python 中继承的语法如下:

class Parent:
pass

class Child(Parent):
pass
代码块12345
  • 在第 1 行,定义了父类 Parent;
  • 在第 4 行,定义了子类 Child,语法 Child(Parent) 表示类 Child 继承于类 Parent。

子类继承父类的属性和方法,使得子类具有父类的属性和方法,从而实现代码重用;同时,子类可以增加自己特有的方法。例如,下图中定义了 3 个类,类 Teacher 与类 Student 继承于类 Person,如图所示:

  • 父类 Person 定义了属性 name 和 age,定义了方法 introduce,这些属性和方法被子类继承
  • 子类 Teacher 定义了自己特有的属性 salary,定义了自己特有的方法 showSalary
  • 子类 Student 定义了自己特有的属性 grade,定义了自己特有的方法 showGrade

1. 在子类中增加属性和方法

1.1 编写父类 Person

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce(self):
print('My name is', self.name)
print('My age is', self.age)
代码块12345678
  • 在第 2 行,定义构造方法 __init__,设置属性 name 和 age
  • 在第 6 行,定义方法 introduce,打印属性 name 和 age

1.2 编写子类 Teacher

class Teacher(Person):
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary

def showSalary(self):
print('My salary is', self.salary)
代码块12345678
  • 在第 1 行,通过语法 Teacher(Person),定义继承于 Person 的类 Teacher
  • 在第 5 行,在构造方法中,增加类 Teacher 特有的属性 salary
  • 在第 7 行,定义方法 showSalary,打印属性 salary

1.3 编写子类 Student

class Student(Person):
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade

def showGrade(self):
print('My grade is', self.grade)
代码块12345678
  • 在第 1 行,通过语法 Student(Person),定义继承于 Person 的类 Student
  • 在第 5 行,在构造方法中,增加类 Student 特有的属性 grade
  • 在第 7 行,定义方法 showGrade,打印属性 grade

1.4 创建实例

teacher = Teacher('tom', 30, 5000)
teacher.introduce()
teacher.showSalary()
print()
student = Student('jerry', 10, 90)
student.introduce()
student.showGrade()
代码块1234567
  • 在第 1 行,创建实例 teacher在第 2 行,调用父类方法 introduce在第 3 行,调用自己特有的方法 showSalary
  • 在第 5 行,创建实例 student在第 6 行,调用父类方法 introduce在第 7 行,调用自己特有的方法 showGrade

运行程序,输出如下结果

My name is tom
My age is 30
My salary is 5000

My name is jerry
My age is 10
My grade is 90
代码块1234567
  • 第 1 行到第 3 行,是 teacher 的输出结果
  • 第 5 行到第 7 行,是 student 的输出结果

2. 在子类中调用父类的构造方法

2.1 代码重复的问题

在前面的小节中,类 Person 的构造方法如下:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age
代码块1234

类 Teacher 的构造方法如下:

class Teacher(Person):
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
代码块12345

类 Student 的构造方法如下:

class Student(Person):
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
代码块12345

在这 3 段初始化代码中,存在明显的代码重复,我们希望:

  • 初始化类 Teacher 的属性 name、age、salary 时,可以重用父类 Person 的初始化代码
  • 初始化类 Student 的属性 name、age、grade 时,可以重用父类 Person 的初始化代码

2.2 编写父类 Person

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce(self):
print('My name is', self.name)
print('My age is', self.age)
代码块12345678
  • 在第 2 行,定义构造方法 __init__,设置属性 name 和 age
  • 在第 6 行,定义方法 introduce,打印属性 name 和 age

2.3 编写子类 Teacher

class Teacher(Person):
def __init__(self, name, age, salary):
Person.__init__(self, name, age)
self.salary = salary

def showSalary(self):
print('My salary is', self.salary)
代码块1234567
  • 在第 1 行,通过语法 Teacher(Person),定义继承于 Person 的类 Teacher
  • 在第 3 行,通过语法 Person.__init(self, name, age)__ 调用父类的构造方法 __init__,对属性 name 和 age 进行设置
  • 在第 4 行,在构造方法中,增加类 Teacher 特有的属性 salary
  • 在第 6 行,定义方法 showSalary,打印属性 salary

2.4 编写子类 Student

class Student(Person):
def __init__(self, name, age, grade):
Person.__init__(self, name, age)
self.grade = grade

def showGrade(self):
print('My grade is', self.grade)
代码块1234567
  • 在第 1 行,通过语法 Student(Person),定义继承于 Person 的类 Student
  • 在第 3 行,通过语法 Person.__init(self, name, age)__ 调用父类的构造方法 __init__,对属性 name 和 age 进行设置
  • 在第 4 行,在构造方法中,增加类 Student 特有的属性 grade
  • 在第 6 行,定义方法 showGrade,打印属性 grade

2.5 创建实例

teacher = Teacher('tom', 30, 5000)
teacher.introduce()
teacher.showSalary()
print()
student = Student('jerry', 10, 90)
student.introduce()
student.showGrade()
代码块1234567
  • 在第 1 行,创建实例 teacher在第 2 行,调用父类方法 introduce在第 3 行,调用自己特有的方法 showSalary
  • 在第 5 行,创建实例 student在第 6 行,调用父类方法 introduce在第 7 行,调用自己特有的方法 showGrade

运行程序,输出如下结果

My name is tom
My age is 30
My salary is 5000

My name is jerry
My age is 10
My grade is 90
代码块1234567
  • 第 1 行到第 3 行,是 teacher 的输出结果
  • 第 5 行到第 7 行,是 student 的输出结果

3. 多继承

定义一个新的 class 的时候,可以从多个现有的 class 继承,如果继承多个父类,称为多继承。Python 中多继承的语法如下:

class Father:
pass

class Mother:
pass

class Child(Father, Mother):
pass
代码块12345678
  • 在第 1 行,定义了父类 Father
  • 在第 4 行,定义了父类 Mother
  • 在第 7 行,定义了子类 Child,它继承于两个父类:Father 和 Mother

子类继承所有父类的属性和方法,从而实现代码重用。

4. 多继承的例子

4.1 概述

本节构造 3 个类:Father、Mother 和 Child,Child 继承于两个类 Father 和 Mother,它继承了这两个类的属性和方法,同时有自己特有的属性和方法。

4.2 编写父类 Father

class Father:
def __init__(self, father_attr):
self.father_attr = father_attr

def father_method(self):
print('father_attr =', self.father_attr)
代码块123456
  • 在第 3 行,定义类 Father 的属性 father_attr
  • 在第 5 行,定义类 Father 的方法 father_method

4.3 编写父类 Mother

class Mother:
def __init__(self, mother_attr):
self.mother_attr = mother_attr

def mother_method(self):
print('mother_attr =', self.mother_attr)
代码块123456
  • 在第 3 行,定义类 Mother 的属性 mother_attr
  • 在第 5 行,定义类 Mother 的方法 mother_method

4.4 编写子类 Child

class Child(Father, Mother):
def __init__(self, father_attr, mother_attr, child_attr):
Father.__init__(self, father_attr)
Mother.__init__(self, mother_attr)
self.child_attr = child_attr

def child_method(self):
print('child_attr =', self.child_attr)
代码块12345678
  • 在第 1 行,定义类 Child,继承于 Father 和 Mother
  • 在第 3 行,调用父类 Father 的构造方法
  • 在第 4 行,调用父类 Mother 的构造方法
  • 在第 7 行,定义类 Child 的方法 child_method

4.5 创建实例

child = Child('Father', 'Mother', 'Child')        
child.father_method()
child.mother_method()
child.child_method()
代码块1234
  • 在第 1 行,创建实例 Child
  • 在第 2 行,调用继承于父类 Father 的方法 father_method
  • 在第 3 行,调用继承于父类 Mother 的方法 mother_method
  • 在第 4 行,调用类自己的方法 child_method

程序输出结果如下:

father_attr = Father
mother_attr = Mother
child_attr = Child
代码块123

欢迎关注「慕课网」,发现更多IT圈优质内容,分享干货知识,帮助你成为更好的程序员!

标签:__,name,Person,继承,什么,self,Python,父类,age
From: https://blog.51cto.com/u_15771948/6116949

相关文章

  • Python中[-1]、[:-1]、[::-1]、[n::-1]、[:,:,0]、[…,0]、[…,::-1] 的理解
    在python中会出现[-1]、[:-1]、[::-1]、[n::-1]、[:,:,0]、[…,0]、[…,::-1],他们分别是什么意思呢,这里就来详尽的说一下:下面的a=[1,2,3,4,5][-1]:列表最后一项[:-1]......
  • python开发环境使用和编程初体验
    #实验任务1 print('hey,u')print('hey','u')x,y,z=1,2,3print(x,y,z) print('x=%d,y=%d,z=%d'%(x,y,z)) print('x={},y={},z......
  • Python列表、元组、字典和集合的用法
    1.列表标志符号是[],元素可以修改、删除和新增1.1提取元素(索引从0开始计算)testList=['A','B','C',1,'D']print(testList[1])#打印索引区间[1,4)print(testList[0:3]......
  • 吴恩达机器学习ex2 python实现
    这个项目包含了吴恩达机器学习ex2的python实现,主要知识点为逻辑回归、正则化,题目内容可以查看数据集中的ex2.pdf代码来自网络(原作者黄广海的github),添加了部分对于题意的......
  • python一行代码,有局限性
    classA:defadd(self,t):print(t,'走了a')classB:defadd(self,t):print(t,'走了b')host_names=A()ips=B()classIpChec......
  • 吴恩达机器学习ex1 python实现
     这个项目包含了吴恩达机器学习ex1的python实现,主要知识点为线性回归,题目内容可以查看数据集中的ex1.pdf代码来自网络(原作者黄广海的github),添加了部分对于题意的中......
  • new Vue的时候到底做了什么
    Vue加载流程1.初始化的第一阶段是Vue实例也就是vm对象创建前后:首先Vue进行生命周期,事件初始化发生在beforeCreate生命周期函数前,然后进行数据监测和数据代理的初始化,也就......
  • 【建造者设计模式详解】Java/JS/Go/Python/TS不同语言实现
    简介建造者模式(BuilderPattern),也叫生成器模式,属于创建型模式。它使用多个简单的对象一步一步构建成一个复杂的对象。它允许你使用相同的创建代码生成不同类型和形式的对......
  • 实验1 Python开发环境使用和编程初体验
    Task1-1代码源码#print输出的几种用法#用法1:用于输出单个字符串或单个变量print('hey,u')#用法2:用于输出多个数据项,用逗号分隔print('hey','u')x,y,z=1,2,3pri......
  • 转:什么是SIT测试,、UAT测试?
    SIT测试SIT(SystemIntegrationTesting)系统集成测试,也叫做集成测试,是软件测试的一个术语,在其中单独的软件模块被合并和作为一个组测试。它在单元测试以后和在系统测试之......