python多态性_Python中的多态性

什么是多态性:多态性这个词意味着有多种形式。在编程中,多态性意味着对不同类型使用相同的函数名。

内置多态函数示例:

# Python program to demonstrate in-built poly-

# morphic functions

# len() being used for a string

print(len("geeks"))

# len() being used for a list

print(len([10, 20, 30]))

输出:

5

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

类方法的多态性:

下面的代码显示python如何以相同的方式使用两种不同的类类型。 我们创建一个for循环,该循环遍历对象的元组。 然后调用方法而不必担心每个对象是哪种类类型。 我们假设这些方法实际上存在于每个类中。

class India():

def capital(self):

print("New Delhi is the capital of India.")

def language(self):

print("Hindi the primary language of India.")

def type(self):

print("India 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_ind = India()

obj_usa = USA()

for country in (obj_ind, obj_usa):

country.capital()

country.language()

country.type()

输出:

New Delhi is the capital of India.

Hindi the primary language of India.

India 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(),它们分别在“India”和“USA”两个类别中定义。 接下来,让我们创建“India”和“USA”类的实例化(如果我们还没有的话)。有了这些,我们可以使用相同的func()函数调用它们的动作:

def func(obj):

obj.capital()

obj.language()

obj.type()

obj_ind = India()

obj_usa = USA()

func(obj_ind)

func(obj_usa)

代码:使用函数实现多态

class India():

def capital(self):

print("New Delhi is the capital of India.")

def language(self):

print("Hindi the primary language of India.")

def type(self):

print("India 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_ind = India()

obj_usa = USA()

func(obj_ind)

func(obj_usa)

输出:

New Delhi is the capital of India.

Hindi the primary language of India.

India is a developing country.

Washington, D.C. is the capital of USA.

English is the primary language of USA.

USA is a developed country.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值