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.

你可能感兴趣的:(python多态性)