ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

31. Python中的多态(Polymorphism)

2022-02-20 13:32:34  阅读:266  来源: 互联网

标签:Polymorphism obj USA Python self 多态 China print def


《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.

标签:Polymorphism,obj,USA,Python,self,多态,China,print,def
来源: https://blog.csdn.net/bai666ai/article/details/123029807

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有