如何在Python中从超类创建子类?

如何在Python中从超类创建子类?

在面向对象编程中,子类可以继承超类(也称为父类或基类)的属性和方法。在Python中,可以使用关键字class来定义一个类。在定义一个子类时,我们可以从一个或多个超类来创建它。如果多个超类中有相同的方法名,Python将按照一定的顺序来查找并使用这些方法。在本文中,我们将讨论如何在Python中从超类创建子类。

更多Python文章,请阅读:Python 教程

了解继承

在面向对象编程中,一个类可以从另一个类继承属性和方法。这个被继承的类被称为超类或父类,而继承属性和方法的类被称为子类。子类可以使用它继承的属性和方法,也可以重写或添加自己的方法。

在Python中,我们可以使用关键字class来定义一个类。新建的类可以从已有的类中继承属性和方法。

下面是一个简单的例子,其中一个超类Animal中包含eat()、sleep()和wake_up()方法,而一个子类Cat从Animal类中继承了这些方法。

class Animal:
    def eat(self):
        print("The animal is eating.")
    def sleep(self):
        print("The animal is sleeping.")
    def wake_up(self):
        print("The animal woke up.")


class Cat(Animal):
    def meow(self):
        print("The cat is meowing.")

现在我们可以创建一个猫的实例,并调用方法:

tom = Cat()
tom.eat() # 输出 "The animal is eating."
tom.meow() # 输出 "The cat is meowing."

在上面的代码中,我们创建了一个Cat类,它继承了Animal类的方法。现在我们可以创建一个Cat的实例tom,它拥有Animal类中的所有方法,以及Cat类中特有的meow()方法。

如何创建一个从超类继承的子类

在Python中,我们可以用以下的方式来创建一个从超类继承的子类:

class ChildClassName(SuperClassName):
    def __init__(self, child_attr, super_attr):
        super().__init__(super_attr)
        self.child_attr = child_attr

在上面的代码中,我们定义了一个名字为ChildClassName的子类,它带有一个名为child_attr的属性。在这个类的定义中,我们通过使用super()函数来引用它的超类,并在子类的__init__()方法中调用超类的__init__()方法,从而继承超类的属性和方法。

这里的两个参数都是实例化时可能用到的参数。假设我们有一个名为Dog的超类,它带有一个名为name的属性。Dog类如下所示:

class Dog:
    def __init__(self, name):
        self.name = name
    def bark(self):
        print("The dog is barking.")

既然我们有了一个基类,我们现在可以从它创建一个子类,比如,我们创建一个名为SledDog的类,并从Dog中继承。

class SledDog(Dog):
    def pull_sled(self):
        print("The dog is pulling the sled.")

然后,我们可以创建一个SledDog类的实例,并分别访问两个类的方法:

ice = SledDog("Ice")
print(ice.name)  # 输出"Ice"
ice.bark()   # 输出"The dog is barking."
ice.pull_sled() # 输出"The dog is pulling the sled."

在上面的代码中,我们从Dog类创建了一个名为SledDog的子类,并通过使用super()函数引用超类。子类的方法pull_sled()是在子类中定义的,而从Dog继承的方法__init__()和bark()是由超类提供的。注意,SledDog类中的__init__()方法和Dog类中的__init__()方法实际上是完全相同的,因为我们没有对__init__()方法进行任何重写。

接下来是一个稍微复杂一些的例子

在本节中,我们将定义一个名为Vehilce的超类和两个子类,Car和Truck。

首先,我们定义Vehilce类:

class Vehicle:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.weight = 0

    def get_make(self):
        return self.make

    def get_model(self):
        return self.model

    def get_year(self):
        return self.year

    def get_weight(self):
        return self.weight

    def set_weight(self, weight):
        self.weight = weight

class Car(Vehicle):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.weight = 2000

    def description(self):
        print(self.get_make(), self.get_model(), "(", self.get_year(), ")", "- a car weighing", self.get_weight(), "pounds.")

class Truck(Vehicle):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.weight = 4000

    def description(self):
        print(self.get_make(), self.get_model(), "(",self.get_year(),"), - a truck weighing", self.get_weight(), "pounds.")

    def load(self, weight):
        self.weight += weight
        print("The truck is now carrying a load weighing", weight, "pounds.")

在这个例子中,我们定义了一个Vehilce类,它有一个__init__()方法和一些简单的getter和setter方法。然后,我们定义了两个子类,Car和Truck。这两个子类都从Vehicle类中继承了__init__()方法和get/set方法,并对重量进行了初始化。子类中的description()方法用于打印车的描述,而Truck类中的load()方法用于添加车的载荷。

现在我们可以创建一个Car类的实例,并调用它的方法:

my_car = Car("Toyota", "Prius", 2018)
my_car.description()

输出:Toyota Prius ( 2018 ) – a car weighing 2000 pounds.

我们也可以创建一个Truck类的实例,并添加载荷:

my_truck = Truck("Ford", "F150", 2021)
my_truck.description()
my_truck.load(1000)
my_truck.description()

输出:Ford F150 ( 2021 ), – a truck weighing 4000 pounds.

输出:Ford F150 ( 2021 ), – a truck weighing 5000 pounds.

在上面的代码中,我们创建了一个Car和一个Truck类的实例,并且它们都是从Vehilce类继承的。我们使用super()函数来调用超类的构造函数,并初始化了它们的重量。然后,我们调用了它们的description()方法来打印出它们的描述。最后,我们通过用load()方法添加载荷,打印出了Truck类实例的新描述。

结论

在Python中,我们可以用class关键字来定义一个类。一个类可以从另一个类继承属性和方法,这个被继承的类被称为父类或超类,而继承属性和方法的类被称为子类。子类可以使用它继承的属性和方法,也可以重写或添加自己的方法。在Python中,我们可以使用super()来引用超类,使用子类的构造函数(__init__())中的super().__init__()语句来调用超类的构造函数,从而继承超类的属性和方法。在本文中,我们讨论了如何在Python中从超类创建一个子类,并提供了一个简单的和稍微复杂一些的例子,希望可以帮助你更好地理解从超类创建子类的方法和使用。对于想要实现继承的开发者来说,这将是非常有用的知识点。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程