Python教程-Python super() 函数

正如我们都知道的,Python 是一门面向对象的编程语言。因此,Python 遵循所有面向对象编程(OOP)的概念,其中之一就是继承。
在使用继承的概念时,我们可以在继承或子类中使用 super() 函数引用父类。在子类中使用的 super() 函数返回父类的一个临时创建对象,使我们能够访问子类中存在的所有方法。
super() 函数的好处:
使用 super() 函数在子类中有以下好处:
- 在使用 super() 函数时,我们不需要记住父类的类名。这是因为我们不必指定父类的名称来访问其中的方法。
- 我们可以在单一继承和多重继承中使用 super() 函数。
- Python 中的 super() 函数实现了代码的可重用性和模块化,因为我们不需要一遍又一遍地重写整个函数。
- Python 中的 super() 函数被称为动态函数,因为我们都知道 Python 是一门动态类型的编程语言。
使用 super() 函数的约束:
使用 Python 程序中的 super() 函数必须遵循以下三个约束:
- super() 函数中的参数与我们调用的函数中的参数应匹配。
- 我们在使用该方法后的每次出现的方法都应该包括 super() 关键字。
- 我们必须指定 super() 函数引用的类和方法。
现在,我们知道可以在 Python 中的两种继承类型,即单一继承和多重继承中使用 super() 函数。因此,我们将分别学习如何在这两种继承类型中使用 super() 函数,并附带示例。
在 Python 中的单一继承中使用 super() 函数
在这个示例中,我们将使用动物作为单一继承示例的参考。
猫、马、牛、狗等都属于动物界类。它们也共享一些共同的特征:
- 它们都是宠物动物。
- 它们都有四条腿和一条尾巴。
- 作为动物界类的一部分,它们都是哺乳动物。
因此,我们可以说类猫、类马和类狗都是类动物界的子类。这是一个单一继承的例子,因为所有子类(类猫、类马和类狗)只从一个父类,即类动物界继承。现在,让我们看下面的程序。
示例 -
# Define parent class animalia
class Animalia:
# define construcors for parent animalia class
def __init__(self):
self.Legs = 4
self.adomestic = True
self.atail = True
self.amammals = True
# define mammal class as child class
def aMammal(self):
if self.amammals:
print("The given animal is a mammal type .")
# define domestic class as child class
def aDomestic(self):
if self.adomestic:
print("The given animal is a domestic animal type.")
# define dog class
class Dog(Animalia):
def __init__(self):
super().__init__() # using super() function to access class methods
def isMammal(self):
super().aMammal() # using mammal class
# define cat class
class Cat(Animalia):
def __init__(self):
super().__init__()
def isMammal(self):
super().aDomestic() # using domestic class
# define horse class
class Horse(Animalia):
def __init__(self):
super().__init__()
def TailandLegs(self): # using tail and legs class
if self.atail and self.Legs == 4:
print("The given animal has four legs and a tail")
# Taking the driver's code for defined classes
Tommy = Dog()
Tommy.aMammal()
Tom = Cat()
Tom.aDomestic()
Burno = Horse()
Burno.TailandLegs()
输出:
The given animal is a mammal type.
The given animal is a domestic animal type.
The given animal has four legs and a tail.
解释:
在上面的代码中,我们定义了动物界作为父类,并从中继承了家养、尾巴和腿以及哺乳动物类。之后,我们定义了猫、马和狗类,并在其中使用了 super() 函数。借助这些类中的 super() 函数,我们可以在猫、马和狗类中访问动物界类的方法。
在 Python 中的多重继承中使用 super() 函数
在此示例中,我们将使用一个父类,即哺乳动物类。然后,我们将从哺乳动物类继承“可以飞”和“可以游泳”类。
这些类将表示给定的哺乳动物是否能飞或不能飞以及是否能游泳或不能游泳。之后,我们将定义一个动物类,它将从“可以飞”和“可以游泳”类继承,然后返回给定的动物是否具有定义的特征。
因此,我们可以看到在此处使用的动物类从多个基类继承,因此这是 Python 中多重继承的一个示例。现在,让我们看下面的程序。
示例
# Define Mammal class as parent class
class aMammals():
def __init__(self, name):
print(name, "Is a mammal of animalia class")
# define can fly as child class
class FlyCapable(aMammals):
def __init__(self, FlyCapable_name):
print(FlyCapable_name, "is not capable of flying")
# Calling Parent class Constructor
super().__init__(FlyCapable_name)
# define can swim as child class
class SwimCapable(aMammals):
def __init__(self, SwimCapable_name):
print(SwimCapable_name, "is not capable of swimming")
super().__init__(SwimCapable_name)
# Inherit animalia class from both fly and swim class
class animalia(FlyCapable, SwimCapable):
def __init__(self, name):
super().__init__(name) # using super() function
# Taking driver Code for animalia class
Burno = animalia("Cat")
输出:
Cat is not capable of flying
Cat is not capable of swimming
Cat Is a mammal of animalia class
解释:
在上面的代码中,我们定义了哺乳动物作为父类。之后,我们从哺乳动物类继承了可以飞和可以游泳类。我们使用了可以飞和可以游泳的方法在 animalia 类中,借助 super() 函数。animalia 类从可以飞和可以游泳类继承。