什么是多态性:多态性这个词意味着有多种形式。在编程中,多态性意味着相同的函数名称(但不同的签名)用于不同的类型。主要区别在于函数中使用的数据类型和参数数量。
内置多态函数的示例:
# 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 is the most widely spoken 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 is the most widely spoken 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”两个类中定义。接下来,如果我们还没有“印度”和“美国”类的实例,那么让我们创建它们。有了这些,我们可以使用相同的 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 is the most widely spoken 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)
class Animal: def speak(self): raise NotImplementedError("Subclass must implement this method") class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" # Create a list of Animal objects animals = [Dog(), Cat()] # Call the speak method on each object for animal in animals: print(animal.speak())
Woof! Meow!
原文链接:codingdict.net