Python object-oriented (4) (polymorphism)

Polymorphism refers to: multiple states, that is, when a certain behavior is completed, different objects will be used to obtain different states.

How to understand?

class Animal:
    def speak(self):
        pass


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


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



def make_noise(animal: Animal):
    """制造点噪音,需要传入Animal对象"""
    animal.speak()


# 演示多态,使用2个子类对象来调用函数
dog = Dog()
cat = Cat()

make_noise(dog)        # 输出:汪汪汪
make_noise(cat)        # 输出:喵喵喵

                The same behavior (function), different objects are passed in, and different states are obtained

Polymorphism often acts on inheritance relationships.

for example:

  • The function (method) formal parameter declaration receives the parent class object
  • Actually pass in the subclass object of the parent class to work

Right now:

  • The parent class makes a definition statement
  • subclass to do the actual work
  • to get the same behavior, different states

abstract class (interface)

The speak method of the parent class Animal is an empty implementation

The implications of this design are:

  • The parent class is used to determine which methods
  • The specific method implementation is determined by the subclass

This way of writing is called an abstract class (also called an interface)

  • Abstract class: A class with abstract methods is called an abstract class
  • Abstract method: The method body is an empty implementation (pass) called an abstract method

Why use abstract classes?

After the standard is put forward, different manufacturers realize the requirements of the standard respectively.

An abstract class is like defining a standard,

Contains some abstract methods that require subclasses to implement.

 With polymorphism, complete

  • Abstract parent class design (design criteria)
  • Concrete subclass implementation (implementation standard)

 

# 演示抽象类
class AC:
    def cool_wind(self):
        """制冷"""
        pass

    def hot_wind(self):
        """制热"""
        pass

    def swing_l_r(self):
        """左右摆风"""
        pass


class Midea_AC(AC):
    def cool_wind(self):
        print("美的空调制冷")

    def hot_wind(self):
        print("美的空调制热")

    def swing_l_r(self):
        print("美的空调左右摆风")


class GREE_AC(AC):
    def cool_wind(self):
        print("格力空调制冷")

    def hot_wind(self):
        print("格力空调制热")

    def swing_l_r(self):
        print("格力空调左右摆风")


def make_cool(ac: AC):
    ac.cool_wind()


midea_ac = Midea_AC()
gree_ac = GREE_AC()


make_cool(midea_ac)
make_cool(gree_ac)

 Summarize:

1. What is polymorphism?

Polymorphism refers to the same behavior, using different objects to obtain different states.

For example, define a function (method), declare through the type annotation that the parent class object is required, and actually pass in the subclass object to work, so as to obtain different working states

2. What is an abstract class (interface)

A class that contains abstract methods is called an abstract class. Abstract method refers to: the method (pass) without concrete implementation is called abstract method 3. The role of abstract class

It is mostly used for top-level design (design standards), so that subclasses can implement it concretely.

It is also a soft constraint on subclasses, requiring subclasses to override (implement) some methods of the parent class

And use it in conjunction with polymorphism to obtain different working states.

 

Guess you like

Origin blog.csdn.net/qq1226546902/article/details/131945505