Python语言基础
Python语言进阶
Python数据结构

Python 继承

搞懂Python类继承的用法
继承允许定义继承另一个类的所有方法和属性的类。父类是继承的类,也称为基类。子类是从另一个类继承的类,也称为派生类。

创建父类

创建一个名为 Person 的类,其中包含 firstname 和 lastname 属性以及 printname 方法:
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printinfo(self):
        print("name is %s , age is %d " %(self.name,self.age))

# 实现特定功能的多行代码使用 Animal 来创建对象,然后执行 printinfo 方法:
x = Animal("dog", 6)
x.printinfo()
执行结果:
name is dog , age is 6

创建子类

创建一个名为 Cat 的类,它将从 Animal 类继承属性和方法:
class Cat(Animal):
    pass
注意:如果您不想向该类添加任何其他属性或方法,请使用 pass 关键字。
现在,Cat 类拥有与 Animal 类相同的属性和方法。
使用 Cat 类创建一个对象,然后执行 printinfo 方法:
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printinfo(self):
        print("name is %s , age is %d " %(self.name,self.age))

class Cat(Animal):
    pass # 实现特定功能的多行代码使用 Animal 来创建对象,然后执行 printinfo 方法:
x = Cat("cat", 8)
x.printinfo()
执行结果:
name is cat , age is 8

__init__() 函数

到目前为止,我们已经创建了一个子类,它继承了父类的属性和方法。
我们想要把 __init__() 函数添加到子类(而不是 pass 关键字)。需要注意的是每次使用类创建新对象时,都会自动调用 __init__() 函数。
为 Cat 类添加 __init__() 函数
class Cat(Animal):
    def __init__(self, name, age):
注意:当您添加__init__()函数时,子类将不再继承父的__init__()函数。子的 __init__() 函数会覆盖对父的 __init__() 函数的继承
如需保持父的 __init__() 函数的继承,请添加对父的 __init__() 函数的调用:
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printinfo(self):
        print("name is %s , age is %d " %(self.name,self.age))

class Cat(Animal):
    def __init__(self, name, age):
        Animal.__init__(self, name, age)
x = Cat("cat", 10)
x.printinfo()
执行结果:
name is cat , age is 10

super() 函数

Python 还有一个super() 函数,它会使子类从其父继承所有方法和属性:
通过使用 super() 函数,您不必使用父元素的名称,它将自动从其父元素继承方法和属性。
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printinfo(self):
        print("name is %s , age is %d " %(self.name,self.age))

class Cat(Animal):
    def __init__(self, name, age):
        super().__init__(name, age)
x = Cat("cat", 12)
x.printinfo()
执行结果:
name is cat , age is 12

添加属性

比如把属性color添加到 Cat 类中:
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printinfo(self):
        print("name is %s , age is %d " %(self.name,self.age))

class Cat(Animal):
    def __init__(self, name, age):
        super().__init__(name, age)
        self.color = "block"
x = Cat("cat", 12)
print("name is %s , age is %d , color is %s" %(x.name,x.age,x.color))
执行结果:
name is cat , age is 12 , color is block
在这例子中,block 年应该是一个变量,并在创建 Cat 对象时传递到 Cat 类。为此,请在 __init__() 函数中添加另一个参数:
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printinfo(self):
        print("name is %s , age is %d " %(self.name,self.age))

class Cat(Animal):
    def __init__(self, name, age,color):
        super().__init__(name, age)
        self.color = color
x = Cat("cat", 12,"block")
print("name is %s , age is %d , color is %s" %(x.name,x.age,x.color))
执行结果:
name is cat , age is 12 , color is block

添加方法

把名为 say 的方法添加到 Cat 类:
class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def printinfo(self):
        print("name is %s , age is %d " %(self.name,self.age))

class Cat(Animal):
    def __init__(self, name, age,color):
        super().__init__(name, age)
        self.color = "block"
    def say(self):
        print("name is %s , age is %d , color is %s" %(self.name,self.age,self.color))
x = Cat("cat", 12,"block")
x.say()
执行结果:
name is cat , age is 12 , color is block
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4