Python 继承是面向对象编程语言中一个有趣且重要的概念。继承意味着创建一个新类,该类继承(获取)父类的所有功能,并允许它们添加更多功能。
什么是 Python 继承?
当通过继承功能创建新类时,称为子类(或派生类或子类)。我们从中继承的类称为基类、父类或超类。
父类
在 Python 面向对象编程继承概念中,父类与普通类相同。它包含对许多实例通用的、可重用的属性和方法。例如,在一个组织中,员工详细信息,如年龄、姓名、入职日期和性别,在每个实例中都是相同的。
因此,您可以在父类中声明它们,并在子类中使用它们。同样,一些计算,如加薪百分比,可能相同。因此,我们可以一次在父类中定义一个函数,并可以从多个子类调用它。
子类或派生类
它看起来像一个普通的类,但它继承(继承)了其 Python 父类(从中派生)中提供的所有属性和方法。您还可以创建新方法来执行此子类的必需计算。
通常,我们可以通过将父类作为对象来创建此子类。这意味着创建子类时会带括号。
Python 继承的优点
以下是使用此面向对象编程中的继承的几个优点或好处。
- 它提供了代码的可重用性、可读性和可扩展性。
- 它减少了代码重复。您可以将所有标准方法和属性放在父类中。这些可以被从中派生的子类访问。
- 通过将代码划分为多个类,应用程序看起来更好,并且易于识别错误。
让我们通过多个示例了解如何在实际中使用它。您还可以了解 Python 多重继承、多层继承、issubclass 方法和 isinstance 方法。
Python 类继承示例
在此示例中,我们仅使用 pass 关键字创建了一个父类和一个子类(派生自父类)。您可以将其视为此类继承的语法。
class Employee:
pass
class Department(Employee):
pass
Python 单继承
这是一个单继承的例子。这意味着一个类继承自一个父类。首先,我们声明了一个具有变量 x 和 func_msg() 方法的 Employee。
接下来,我们创建了一个从 Employee 继承的 Department。在此 Department 类中,我们声明了一个变量 a = 250 和 func_message()。
class Employee:
x = 10 # Parent Variable
def func_msg() # Parent Method
class Department(Employee):
a = 250 # Child Variable
def func_message() # Child Method
这是上面示例的扩展。我们没有定义空方法,而是使用了 print 函数来打印来自 Employee 和 Department 的消息。
class Employee:
x = 10
def func_msg(self):
print('Welcome to Employee')
class Department(Employee):
a = 250
def func_message(self):
print('Welcome to Department')
print('This is inherited from Employee')
emp = Employee()
print(emp.x)
emp.func_msg()
print('--------------')
dept = Department()
print(dept.a)
dept.func_message()
单继承输出。
10
Welcome to Employee
--------------
250
Welcome to Department
This is inherited from Employee
单继承分析
- emp = Employee() 创建 Employee 的实例或对象。使用此对象名称,我们可以访问 Employee 的函数和属性。
- print(emp.x) – 打印员工变量 x 的值,即 10。
- emp.func_msg() – 打印消息 Welcome to Employee
- dept = Department() – 它创建 Department 的对象。
- print(dept.a) – 打印 Department 变量 a 的值,即 250。
- dept.func_message() – 打印来自 department 方法 func_message() 的消息
Python 单继承示例 2
这个单继承示例展示了如何使用 Child 对象来访问父函数和属性。对于这个例子,我们创建了一个带有打印欢迎消息的简单 Employee。接下来,我们创建了一个带有 Program pass 关键字的 Department 子类。
class Employee:
def func_msg(self):
print('Welcome to Employee Class')
class Department(Employee):
pass
emp = Employee()
emp.func_msg()
print('--------------')
dept = Department()
dept.func_msg() # Calling Parent Method

dept = Department() – 它创建 Department 的对象。正如我们已经知道的,它继承自 Employee。您可以使用此 dept 对象访问 Employee 和 Department 中的变量和方法。
dept.func_msg() – 它调用父类中存在的 func_msg() 方法。
从子类访问父类变量
在实际中,您可能需要从子类访问或修改父类中定义的变量值和方法。要从子类继承中访问父类的方法或属性,您必须使用 Python 类名和点运算符。例如,ParentClassName.VariableName 或 ParentClassName.MethodName()
在此继承示例中,我们展示了如何从子类访问父类变量。以及如何修改变量值。首先,我们声明了一个变量 x = 10 的 Employee。接下来,我们声明了从 Employee 继承的 Department。
在 Dept 中,我们声明了一个变量 a = 250。接下来,我们调用了 Employee 变量 x 并为其加上了 22(b = Employee.x + 22)。
class Employee:
x = 10
def func_msg(self):
print('Welcome to Employee')
class Department(Employee):
a = 250
b = Employee.x + 22
def func_message(self):
print('Welcome to Department')
def func_changed(self):
print('New Value = ', Employee.x + 449)
emp = Employee()
print(emp.x)
emp.func_msg()
print('--------------')
dept = Department()
print(dept.a)
print(dept.b)
dept.func_message()
dept.func_changed()
从子类访问父类变量输出
10
Welcome to Employee
--------------
250
32
Welcome to Department
New Value = 459
如您所见,从 Employee 对象打印 x 返回 10。但是,来自 Department 对象的 b 返回 10(Employee.x))+ 22 = 32。
Python 继承 init 示例
到目前为止,我们已经创建了继承了父类方法和变量的派生类。在此继承 init 示例中,我们在父类中定义了 init 函数,并在父类函数中使用这些值。在这里,我们使用 pass 关键字声明了一个子类。接下来,我们创建了一个 Department 对象并调用了那些父类方法。
class Employee:
def __init__(self, fullname, age, income):
self.fullname = fullname
self.age = age
self.income = income
def func_msg(self):
print('Welcome to Employee')
def func_information(self):
print('At age', self.age, self.fullname, 'is earning', self.income)
class Department(Employee):
pass
emp = Employee('Suresh', '27', '650000')
emp.func_msg()
emp.func_information()
print('--------------')
dept = Department('Jenny', '25', '850005')
dept.func_msg() # Calling Parent Method func_msg(self)
dept.func_information() # Calling Parent Method func_information(self)
继承 init 输出
Welcome to Employee
At age 27 Suresh is earning 650000
--------------
Welcome to Employee
At age 25 Jenny is earning 850005
Python 继承 init 示例 2
在此继承示例中,我们在父类和派生类中都定义了 init 函数。在派生类中,使用 Employee.__init__(self, fullname, age, income) 来继承父类 Employee 的功能。
class Employee:
def __init__(self, fullname, age, income):
self.fullname = fullname
self.age = age
self.income = income
def func_information(self):
print('At age', self.age, self.fullname, 'is earning', self.income)
class Department(Employee):
def __init__(self, fullname, age, income):
Employee.__init__(self, fullname, age, income)
emp = Employee('John', '27', '650000')
emp.func_information()
print('--------------')
dept = Department('Jenny', '25', '850005')
print(dept.fullname)
dept.func_information()
At age 27 John is earning 650000
--------------
Jenny
At age 25 Jenny is earning 850005
init 示例 3
继承不仅仅是使用现有函数或属性。您可以扩展或覆盖它们。在此示例中,我们在 Department 的 init 中添加了另一个参数,名为 dept_name。
这意味着当您创建 Employee 类的对象时,您需要提供您的姓名、年龄和收入。但是,如果您创建 Department 的对象,您需要提供您的姓名、年龄、收入和部门名称。
class Employee:
def __init__(self, fullname, age, income):
self.fullname = fullname
self.age = age
self.income = income
def func_information(self):
print('At age', self.age, self.fullname, 'is earning', self.income)
class Department(Employee):
def __init__(self, fullname, age, income, dept_name):
Employee.__init__(self, fullname, age, income)
self.dept_name = dept_name
def func_info(self):
print(self.fullname, self.age, 'Working as a',
self.dept_name, 'is earning', self.income)
emp = Employee('John', '27', '650000')
emp.func_information()
print('--------------')
dept = Department('Jenny', '25', '850005', 'Developer')
dept.func_information()
dept.func_info()
At age 27 John is earning 650000
--------------
At age 25 Jenny is earning 850005
Jenny 25 Working as a Developer is earning 850005
Python 中的多层继承
这个编程语言允许您创建多层继承。这意味着子类可以从另一个子类继承。下图显示了多层继承的图示表示。

在此多层继承示例中,首先我们创建了一个具有一个方法的 Main 类。接下来,我们创建了一个从 Main 继承的 Child 类。接下来,我们创建了另一个从 Child(已从 Main 继承)派生的 Child 类。
class MainClass:
def func_message(self):
print('Welcome to Main')
class Child(MainClass):
def func_child(self):
print('Welcome to Child')
print('This is inherited from Main')
class ChildDerived(Child):
def func_Derived(self):
print('Welcome to Derived')
print('This is inherited from Child')
print('------------')
chldev = ChildDerived()
chldev.func_Derived()
print('------------')
chldev.func_child()
print('------------')
chldev.func_message()
------------
Welcome to Derived
This is inherited from Child
------------
Welcome to Child
This is inherited from Main
------------
Welcome to Main
Python 中的多重继承
这个编程语言支持多重继承。这意味着一个子类可以同时从多个父类继承。让我向您展示多重继承的图示表示。

在此多重继承示例中,我们创建了两个超类 Main1 和 Main2。接下来,我们创建了一个从多个父类继承的子类。
class Main1:
def func_main1(self):
print('This Welcome Message is from Main 1')
class Main2:
def func_main2(self):
print('This is an another Message coming from Main 2')
class ChildClass(Main1, Main2):
def func_child(self):
print('This is coming from Child')
chd = ChildClass()
chd.func_main1()
chd.func_main2()
chd.func_child()
This Welcome Message is from Main 1
This is an another Message coming from Main 2
This is coming from Child
Python 继承中的方法重写
在实际中,您可能需要更改父类或基类中定义的方法的程序逻辑。但是,更改它可能会影响从该父类继承的其余类。因此,与其更改父类中的逻辑,不如在子类中定义一个新函数并编写自己的逻辑。
要重写继承中的方法,您必须在子类中定义一个具有相同名称和相同参数数量的新方法。
在此示例中,我们在 Employee 和 Department(子类)中都创建了相同的 func_message() 方法。接下来,我们为 employee 和 department 创建了一个对象并调用了函数名。
如您所见,Employee 对象打印“Welcome to Employee”消息,因为我们没有更改父类中的任何内容。同时,Department 对象正在打印来自子类的消息,而不是打印基类消息。这是因为我们在子类中重写了 func_message() 的定义。
class Employee:
def func_message(self):
print('Welcome to Employee')
class Department(Employee):
def func_message(self):
print('Welcome to Department')
print('This is inherited from Employee')
emp = Employee()
emp.func_message()
print('------------')
dept = Department()
dept.func_message()
Welcome to Employee
------------
Welcome to Department
This is inherited from Employee
Python issubclass 方法
Python issubclass 方法接受两个参数:子类(child)和超类(main)。此方法用于检查子类是否从超类继承,并根据结果返回布尔值 True 或 False。此 issubclass 方法的语法是:issubclass(sub, sup)
class MainClass:
def func_message(self):
print('Welcome to Main')
class Child(MainClass):
def func_child(self):
print('This class is inherited from Main')
class ChildDerived(Child):
def func_Derived(self):
print('This class is inherited from Child')
print(issubclass(ChildDerived, Child))
print(issubclass(ChildDerived, MainClass))
print('------------')
print(issubclass(Child, MainClass))
print('------------')
print(issubclass(Child, ChildDerived))
issubclass 函数输出
True
True
------------
True
------------
False
Python isinstance 方法
Python isinstance 方法也接受两个参数,分别是对象名和类名。如果对象(第一个参数)是(第二个参数)的实例,则此 isinstance 方法返回 True;否则返回 False。此特殊方法的语法是 isinstance(Object, Class)。
class MainClass:
def func_message(self):
print('Welcome to Main')
class Child(MainClass):
def func_child(self):
print('This is from Main')
class ChildDerived(Child):
def func_Derived(self):
print('This is from Child')
mn = MainClass()
print(isinstance(mn, MainClass))
mn.func_message()
print('------------')
chd = Child()
print(isinstance(chd, Child))
print(isinstance(chd, MainClass))
print(isinstance(chd, ChildDerived))
chd.func_child()
chd.func_message()
print('------------')
dev = ChildDerived()
print(isinstance(dev, ChildDerived))
print(isinstance(dev, Child))
print(isinstance(dev, MainClass))
dev.func_child()
dev.func_message()
dev.func_Derived()
isinstance 方法输出
True
Welcome to Main
------------
True
True
False
This is from Main
Welcome to Main
------------
True
True
True
This is from Main
Welcome to Main
This is from Child