31. Python中的多态(Polymorphism)

《Python编程的术与道:Python语言入门》视频课程
《Python编程的术与道:Python语言入门》视频课程链接:https://edu.csdn.net/course/detail/27845

多态 (Polymorphism)

多态一词意味着具有多种形式(The word polymorphism means having many forms)。 在编程中,多态意味着相同的函数名称(但签名不同)可以用于不同的类型。

函数签名(function signature):包含了一个函数的信息,包括函数名、参数类型、参数个数、顺序以及它所在的类和命名空间。 函数签名用于识别不同的函数,函数的名字只是函数签名的一部分。 对于不同函数签名的函数,即使函数名相同,解释器(在其它语言的编译器和链接器)都认为它们是不同的函数。

Python内置多态函数示例:

# Python program to demonstrate in-built poly- 
# morphic functions 

# len() being used for a string 
print(len("course")) 

# len() being used for a list 
print(len([10, 20, 30])) 
6
3

用户自定义的多态函数的示例:

# A simple Python function to demonstrate 
# Polymorphism 

def add(x, y, z = 0): 
    return x + y + z 

# Driver code 
print(add(2, 3)) 
print(add(2, 3, 4)) 
5
9

类方法的多态:

class China(): 
    def capital(self): 
        print("Beijing is the capital of China.") 

    def language(self): 
        print("Mandarin is the primary language of China.") 

    def type(self): 
        print("China is a developing country.") 

class USA(): 
    def capital(self): 
        print("Washington, D.C. is the capital of USA.") 

    def language(self): 
        print("English is the primary language of USA.") 

    def type(self): 
        print("USA is a developed country.") 

obj_china = China() 
obj_usa = USA() 
for country in (obj_china, obj_usa): 
    country.capital() 
    country.language() 
    country.type() 
Beijing is the capital of China.
Mandarin is the primary language of China.
China is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

类继承时多态:

在Python中,通过多态,可以在子类中定义与父类中的方法同名的方法。 在继承中,子类从父类继承方法。 但是,可以修改从父类继承的子类中的方法。 在从父类继承的方法不太适合子类的情况下,这特别有用。 在这种情况下,我们将在子类中重新实现该方法。 在子类中重新实现方法的过程称为“方法重写”。

class Bird: 
    
    def intro(self): 
        print("There are many types of birds.") 

    def flight(self): 
        print("Most of the birds can fly but some cannot.") 

class sparrow(Bird): 
    def flight(self): 
        print("Sparrows can fly.") 

class ostrich(Bird): 
    def flight(self): 
        print("Ostriches cannot fly.") 
 
obj_bird = Bird() 
obj_spr = sparrow() 
obj_ost = ostrich() 

obj_bird.intro() 
obj_bird.flight() 

obj_spr.intro() 
obj_spr.flight() 

obj_ost.intro() 
obj_ost.flight() 
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

函数和对象的多态:

也可以创建一个可以接受任何对象的函数,从而实现多态。 在此示例中,我们创建一个名为 func()的函数,该函数将使用一个称为 obj的对象。 尽管我们使用的是 obj名称,但是任何实例化的对象都可以在此函数中调用。 接下来,让函数使用传递给它的 obj对象做些事情。 在这种情况下,让我们调用三种方法,即capital(),language()和type(),它们分别在China和USA两个类中定义。 接下来,让我们创建China和USA类的实例化。 有了这些,我们可以使用相同的func()函数调用它们的动作:

使用函数实现多态:

class China(): 
    def capital(self): 
        print("Beijing is the capital of China.") 

    def language(self): 
        print("Mandarin is the primary language of China.") 

    def type(self): 
        print("China is a developing country.") 

class USA(): 
    def capital(self): 
        print("Washington, D.C. is the capital of USA.") 

    def language(self): 
        print("English is the primary language of USA.") 

    def type(self): 
        print("USA is a developed country.") 

def func(obj): 
    obj.capital() 
    obj.language() 
    obj.type() 

obj_china = China() 
obj_usa = USA() 

func(obj_china) 
func(obj_usa) 

Beijing is the capital of China.
Mandarin is the primary language of China.
China is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

你可能感兴趣的:(Python语言入门,python,开发语言,后端,人工智能)