python继承及super()函数

目录

一、python继承

二、super() 函数理解

1、super的工作原理

2、举例


一、python继承


        python中继承是通过类来实现的。在定义子类时,可以在类的声明语句中通过 "class 子类名(父类名):" 的方式指定父类。这样就可以使子类继承父类的属性和方法。

class 子类名(父类1, 父类2, ..., 父类n):
    ......

示例:

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return "Animal"

class Dog(Animal):
    def speak(self):
        return f"Dog: {self.name}"

dog = Dog("lucky")  # 继承父类Animal的 init
print(dog.speak())  # 输出: Dog: lucky

二、super() 函数理解


        super()

你可能感兴趣的:(python脚本,开发语言,python,pycharm)