Python super()函数

众所周知,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 级继承了会游泳和会飞的级别。


原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/248834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 13:30
下一篇 2024-12-12 13:30

相关推荐

  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29

发表回复

登录后才能评论