在面向对象的程序设计中,定义一个新的 class 的时候,可以从某个现有的 class 继承,新的 class 称为子类,而被继承的 class 称为基类、父类或超类。
Python 中继承的语法如下:
class Parent:passclass Child(Parent):pass
在第 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 = agedef introduce(self):print('My name is', self.name)print('My age is', self.age)
在第 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 = salarydef showSalary(self):print('My salary is', self.salary)
在第 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 = gradedef showGrade(self):print('My grade is', self.grade)
在第 1 行,通过语法 Student(Person),定义继承于 Person 的类 Student
在第 5 行,在构造方法中,增加类 Student 特有的属性 grade
在第 7 行,定义方法 showGrade,打印属性 grade
1.4 创建实例
teacher = Teacher('tom', , )teacher.introduce()teacher.showSalary()print()student = Student('jerry', , )student.introduce()student.showGrade()
在第 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
第 1 行到第 3 行,是 teacher 的输出结果
第 5 行到第 7 行,是 student 的输出结果
2. 在子类中调用父类的构造方法
2.1 代码重复的问题
在前面的小节中,类 Person 的构造方法如下:
class Person:def __init__(self, name, age):self.name = name
self.age = age
类 Teacher 的构造方法如下:
class Teacher(Person):def __init__(self, name, age, salary):self.name = name
self.age = age
self.salary = salary
类 Student 的构造方法如下:
class Student(Person):def __init__(self, name, age, grade):self.name = name
self.age = age
self.grade = grade
在这 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 = agedef introduce(self):print('My name is', self.name)print('My age is', self.age)
在第 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 = salarydef showSalary(self):print('My salary is', self.salary)
在第 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 = gradedef showGrade(self):print('My grade is', self.grade)
在第 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', , )teacher.introduce()teacher.showSalary()print()student = Student('jerry', , )student.introduce()student.showGrade()
在第 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
第 1 行到第 3 行,是 teacher 的输出结果
第 5 行到第 7 行,是 student 的输出结果
3. 多继承
定义一个新的 class 的时候,可以从多个现有的 class 继承,如果继承多个父类,称为多继承。Python 中多继承的语法如下:
class Father:passclass Mother:passclass Child(Father, Mother):pass
在第 1 行,定义了父类 Father
在第 4 行,定义了父类 Mother
在第 7 行,定义了子类 Child,它继承于两个父类:Father 和 Mother
子类继承所有父类的属性和方法,从而实现代码重用。
(编辑:莱芜站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|