Python parent class method override

In Python, subclasses can override (override) methods of the parent class. This means that a subclass can define a method with the same name as a superclass method, and when that method is called, the subclass's method will be called instead of the superclass's method.

Here is a simple example showing how to override a method of a parent class in a subclass:

class Animal:
    def make_sound(self):
        print("The animal makes a sound.")

class Dog(Animal):
    def make_sound(self):
        print("The dog barks.")

my_animal = Animal()
my_animal.make_sound()  # 输出 "The animal makes a sound."

my_dog = Dog()
my_dog.make_sound()  # 输出 "The dog barks."

In the above example, we defined an Animal class and a Dog class. The Animal class has a method called make_sound which prints a message. The Dog class inherits from the Animal class and overrides the make_sound method in order to print different messages.

When we create an Animal object and call the make_sound method, the output is "The animal makes a sound.". When we create a Dog object and call the make_sound method, the output is "The dog barks." because the Dog class overrides the make_sound method of the Animal class.

It should be noted that if the subclass overrides the method of the parent class, the method of the subclass will completely replace the method of the parent class. If you need to call the method of the parent class in the subclass, you can use the super() function.

But there are exceptions to everything. We may encounter such a situation, that is, most of the class methods inherited by the subclass from the parent class are suitable for the subclass, but there are individual class methods that cannot be directly copied from the parent class. If this class method is not modified, the subclass object cannot be used. In this case, we need to repeat the method of the parent class in the subclass.

For example, birds usually have wings and can fly, so we can define a class related to birds as follows: class Bird: #birds have wings def isWing(self): print("birds have wings") #birds can fly def fly(self): print("birds can fly") However, for ostriches, although they are also birds and have wings,
they
can
only
run
,
not
fly
. For this situation, you can define ostrich like this:
class Ostrich(Bird):
# Override the fly() method of Bird class
def fly(self):
print("Ostrich can't fly")
You can see that because Ostrich inherits from Bird, the Ostrich class has the isWing() and fly() methods of Bird class. Among them, the isWing() method is also suitable for Ostrich, but fly() is obviously not suitable, so we rewrite the fly() method in the Ostrich class.
Rewriting, sometimes called overriding, is a meaning that refers to modifying the internal implementation of existing methods in a class.

Based on the above 2 pieces of code, add the following code and run it:
class Bird:
#Bird has wings
def isWing(self):
print("Bird has wings")
#Bird can fly
def fly(self):
print("Bird can fly")
class Ostrich(Bird):
#Rewrite the fly() method of the Bird class
def fly(self):
print("Ostrich can't fly")

Create an Ostrich object

ostrich = Ostrich()
#Call the rewritten fly() class method in the Ostrich class. The running result of
ostrich.fly()
is:
ostrich can't fly

Obviously, ostrich calls the overridden fly() class method.
How to call the overridden method
In fact, if we override the class method inherited from the parent class in the subclass, then when the method is called outside the class through the subclass object, Python will always execute the overridden method in the subclass.

This creates a new problem, that is, if you want to call the overridden method in the parent class, what should you do?

It's very simple. As mentioned earlier, a class in Python can be regarded as an independent space, and a class method is actually a function in this space. And if you want to call a function in the class space in the global space, you only need to note the class name when calling the function. For example:
class Bird:
#birds have wings
def isWing(self):
print("birds have wings")
#birds can fly
def fly(self):
print("birds can fly")
class Ostrich(Bird):
# override the fly() method of the Bird class
def fly(self):
print("ostriches can't fly")

Create an Ostrich object

ostrich = Ostrich()
#Call the fly() method in the Bird class
Bird.fly(ostrich)
The program running result is:
the bird can fly

In this program, one thing you need to pay attention to is that if you use the class name to call its class method, Python will not customize the binding value for the first self parameter of the method, so you need to manually assign a value to the self parameter in this calling method.
This method of calling an instance method through the class name is also called an unbound method.

Follow the official account "Station Master Yan Changsheng", read all the tutorials on your mobile phone, and learn anytime, anywhere. It contains a search artifact, which can download books and videos on the whole network for free.

Guess you like

Origin blog.csdn.net/D0126_/article/details/131411571