[Python] object-oriented - polymorphism ( polymorphism concept | polymorphism usage | abstract class concept | code example )





1. Object-oriented - polymorphism




1. The concept of polymorphism


"Polymorphism" refers to a variety of states. When different objects of the same type complete a certain behavior, they will get different states;

Polymorphism is generally implemented through inheritance and method rewriting, multiple subclasses inherit the same parent class,

These subclass objects override the methods of the parent class to implement different logic,

Assign different subclass objects to the parent class type variable, and execute different logic when calling the overridden parent class method, and realize polymorphism at this time;


The subclass overrides the method of the parent class, which means that the subclass can define a method with the same name as the parent class, but the implementation method can be different;

When calling this method through a subclass object, the method of the subclass will be executed instead of the method of the parent class;

This behavior enables subclasses to respond differently to the same message, realizing polymorphism;


2. Polymorphic usage


"Polymorphism" is realized through inheritance relationship;

Polymorphic usage rules:

  • The parent class makes member declarations, most of which are member method declarations;
  • Subclasses override member methods to do the actual work;
  • Assign different types of subclass instance objects to multiple parent class type variables;
  • Executing the same method of the parent class instance object, the results obtained are different;

3. Code example - polymorphism


In the code below,

The Animal class is the parent class, which defines the behavior make_sound method,

The Dog class and the Cat class inherit the Animal class, and rewrite the make_sound method of the Animal parent class,

For the variable type annotated as Animal type, assign the Animal instance object, the Dog instance object, the Cat instance object respectively,

When the make_sound method of these three objects is called, different operations will be performed;

  • Animal instance object prints "animal pronunciation";
  • The Dog instance object prints "Wow";
  • The Cat instance object prints "meow";

Code example:

"""
面向对象 - 多态
"""


class Animal:
    name = "Animal"
    age = 0

    def make_sound(self):
        print("动物发音")


class Dog(Animal):
    def make_sound(self):
        print("汪汪")


class Cat(Animal):
    def make_sound(self):
        print("喵喵")


animal: Animal = Animal()
animal.make_sound()

print("")

dog: Animal = Dog()
dog.make_sound()

print("")

cat: Animal = Cat()
cat.make_sound()

Results of the :

Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
动物发音

汪汪

喵喵

Process finished with exit code 0

insert image description here





2. Abstract class




1. Abstract concept


The parent class only defines an empty method, and the method body is passwithout specific implementation;

  • The parent class only defines which methods
  • Subclasses are responsible for implementing specific method logic

This kind of parent class is "abstract class";


The method body is empty, that is, the method of pass, called "abstract method";

A class with "abstract methods" is called an abstract class;


2. Code example - abstract class


Define the abstract class Animal class,

In the Animal class, the abstract method make_sound method is defined, the method body of this method is empty, it is pass, without any logic;

The subclass Cat / Dog inherits the Animal class, implements the make_sound method, and performs different operations respectively;


Code example:

"""
面向对象 - 多态
"""


class Animal:
    def make_sound(self):
        pass


class Dog(Animal):
    def make_sound(self):
        print("汪汪")


class Cat(Animal):
    def make_sound(self):
        print("喵喵")


dog: Animal = Dog()
dog.make_sound()

print("")

cat: Animal = Cat()
cat.make_sound()

Results of the :

Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
汪汪

喵喵

Process finished with exit code 0

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/131775526