四时宝库

程序员的知识宝库

Python中 class的 继承(python中类的继承)

Python中 class的 继承

了解 Python 类继承,这是面向对象编程的核心概念之一。我们添加了足够的示例来为您提供实践经验。

能够编写使用继承的 Python 程序。您将了解 super() 函数,它允许您访问父类的方法和属性。此外学习如何扩展类并覆盖其方法和属性。

类中的 Python 继承解释

  • Python 类继承 – 简介继承对类意味着什么?继承的目的是什么?如何在 Python 中实现继承?
  • Python继承示例创建基类出租车和子类车辆Python 继承的 UML 图
  • 方法super() 在 Python 中做了什么?super() 方法如何工作?
  • Python 继承和 OOP

Python 继承和 OOP 概念

Python 类继承 – 简介

继承是所有面向对象的编程语言(如C++、Java 和 Python)中可用的通用概念。

它表示一个类与另一个类的关系。继承背后的机制使类从其父类派生所有功能。虽然子类可以有自己的。

我们将介绍 Python 中的多重继承概念(带有示例)。

继承对类意味着什么?

继承是面向对象编程的核心功能,它通过添加新功能来扩展现有类的功能。您可以将其与现实生活中的情况进行比较,当孩子除了添加自己的财产外,还继承了父母的财产。他甚至可以从父母那里获得姓氏(第二个名字)。

继承的目的是什么?

通过使用继承功能,我们可以拥有一个具有旧属性的新蓝图,但无需对原始蓝图进行任何更改。我们将新类称为派生类或子类,而旧类成为基类或父类。

如何在 Python 中实现继承?

可以使用以下语法引入继承。

class ParentClass:
  Parent class attributes
  Parent class methods
class ChildClass(ParentClass):
  Child class attributes
  Child class methods

继承会自动为代码带来可重用性,因为派生类已从基类获取所有内容。

Python继承示例

为了理解继承的应用,让我们考虑以下示例。

创建基类出租车和子类车辆

我们有一个基类出租车,它有一个子类(儿童)车辆。

class Taxi:

    def __init__(self, model, capacity, variant):
        self.__model = model      # __model is private to Taxi class
        self.__capacity = capacity
        self.__variant = variant

    def getModel(self):          # getmodel() is accessible outside the class
        return self.__model

    def getCapacity(self):         # getCapacity() function is accessible to class Vehicle
        return self.__capacity

    def setCapacity(self, capacity):  # setCapacity() is accessible outside the class
        self.__capacity = capacity

    def getVariant(self):         # getVariant() function is accessible to class Vehicle
        return self.__variant

    def setVariant(self, variant):  # setVariant() is accessible outside the class
        self.__variant = variant

class Vehicle(Taxi):

    def __init__(self, model, capacity, variant, color):
        super().__init__(model, capacity, variant)
        self.__color = color

    def vehicleInfo(self):
        return self.getModel() + " " + self.getVariant() + " in " + self.__color + " with " + self.getCapacity() + " seats"

# In method getInfo we can call getmodel(), getCapacity() as they are 
# accessible in the child class through inheritance

v1 = Vehicle("i20 Active", "4", "SX", "Bronze")
print(v1.vehicleInfo())
print(v1.getModel()) # Vehicle has no method getModel() but it is accessible via Vehicle class

v2 = Vehicle("Fortuner", "7", "MT2755", "White")
print(v2.vehicleInfo())
print(v2.getModel()) # Vehicle has no method getModel() but it is accessible via Vehicle class

请注意,我们没有在 Vehicle 类中指定 getName() 方法,但我们可以访问它。这是因为 Vehicle 类从 Taxi 类继承了它。

上述示例的输出如下所示。

# output
i20 Active SX in Bronze with 4 seats
i20 Active
Fortuner MT2755 in White with 7 seats
Fortuner

练习 Python:Python 中的数据类练习

Python 继承的 UML 图

为了更清楚,您可以参考上面提到的示例的 Python 继承的 UML 图。

super() 方法

super() 在 Python 中做了什么?

super() 方法允许我们访问级联到类对象的继承方法。

在前面的示例中,我们在子类 <Vehicle> 的构造函数中使用了 super() 方法。它调用基类 <Taxi> 的函数。

super() 方法如何工作?

假设您必须调用基类中的方法,即子类中定义的 vehicleInfo(),那么您可以使用以下代码。

super().vehicleInfo()

同样,可以使用下面的代码从子(子)类__init__调用基类构造函数。

super().__init__()

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接