首页 > 编程知识 正文

Python super()函数

时间:2023-11-25 15:30:24 阅读:309286 作者:NCFV

众所周知,Python 是一种面向对象的编程语言。因此,Python 遵循了 OOPs 的所有概念,其中一个概念就是继承。

在使用继承概念时,我们可以在继承类或子类中使用 super()函数来引用父类。我们在子类中使用的 super()函数返回超类的一个临时创建的对象,它允许我们访问它在子类中的所有方法。

super()功能的优势:

以下是在子类中使用 super()函数的好处:

  • 使用 super()函数时,我们不需要记住父类的名字。这是因为我们不必指定父类的名称来访问其中存在的方法。
  • 我们可以使用 super() 函数,具有单遗传和多遗传。
  • Python 中的 super()函数实现了代码的可重用性和模块化,因为我们不需要一次又一次地重写整个函数。
  • Python 中的 super()函数被称为动态函数,因为我们都知道 Python 是一种动态类型的编程语言。

使用 super()函数的限制:

下面是在 Python 程序中使用 super()函数必须遵循的三个约束:

  • 参数在 super()函数中给出,我们调用的函数中的参数应该匹配。
  • 我们正在使用的方法的每次出现都应该在我们使用之后包含 super()关键字。
  • 我们必须指定其中存在的类和方法,它们由 super()函数引用。

现在,正如我们所知,我们可以在 Python 中的两种继承类型中使用 super()函数,即单一继承和多重继承。因此,我们将学习如何在两种类型的继承中分别使用 super()函数,并给出一个例子。

在 Python 的单继承中使用 super()函数

在这个例子中,我们将使用动物作为单遗传例子的参考。

猫、马、牛、狗等。,都是动物类的一部分。它们都有一些共同的特点:

  • 它们都是宠物。
  • 它们都有四条腿和一条尾巴。
  • 作为动物界的一部分,它们也都是哺乳动物。

所以,我们可以说类猫、类马、和类狗是动物界的子类。这是一个单一继承的例子,因为所有的子类(猫类、马类和狗类)都只从一个单一的父类继承,即 animalia 类。现在,看看下面的程序。

示例-


# 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.

说明:

在上面的代码中,我们已经将 animalia 定义为父类,并从其继承了家养类、尾腿类和哺乳动物类。之后,我们定义了猫、马和狗类,并在其中使用了超级函数。借助这些类中的 super()函数,我们可以访问猫、马和狗类中 animalia 类的方法。

在 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

说明:

在上面的代码中,我们将哺乳动物定义为父类。之后,我们从哺乳动物班继承了会飞和会游泳的课程。借助 super()函数,我们在 animalia 类中使用了既能飞又能游的方法。animalia 级继承了会游泳和会飞的级别。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。