Share examples of duck classes and polymorphism in Python

零下一度
Release: 2017-06-15 15:59:36
Original
1345 people have browsed it

The following editor will bring you an old-fashioned talk about duck classes and polymorphism in python. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look

1. What is polymorphism

##<1>One type has multiple types Ability

<2>Allows different objects to react flexibly to the same message
<3>Treat each used object in a common way
<4>Non-dynamic languages ​​must pass Implemented through inheritance and interfaces

2. Polymorphism in python

<1>通过继承实现多态(子类可以作为父类来使用)
<2>子类通过重载父类的方法实现多态

class Animal:
  def move(self):
    print(&#39;animal is moving....&#39;)
class Dog(Animal):
  pass
def move(obj):
  obj.move()

>>>move(Animal())
>>>animal is moving....
>>>move(Dog())
>>>animal is moving....

class Fish(Animal):
  def move(self):
    print(&#39;fish is moving....&#39;)
>>>move(Fish())
>>>fish is moving....
Copy after login

3. Dynamic language and Duck typing

<1>The type of variable binding is undefined


<2>Functions and methods can receive parameters of any type


<3>Do not check the provided parameter type when calling a method


<4>Whether the call is successful is determined by the methods and attributes of the parameters. If the call is unsuccessful, it will throw An error occurred


<5>No need to implement the interface

class P:
  def init(self, x, y):
    self.x = x
    self.y = y
  def add(self, oth):
    return P(self.x+oth.x, self.y+oth.y)
  def info(self):
    print(self.x, self.y)
class D(P):
  def init(self, x, y, z):
    super.init(x, y)
    self.z = z

  def add(self, oth):
    return D(self.x+oth.x, self.y+oth.y, self.z+oth.z)
  def info(self):
    print(self.x, self.y, self.z)

class F:
  def init(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

  def add(self, oth):
    return D(self.x+oth.x, self.y+oth.y, self.z+oth.z)
  
  def info(self):
    print(self.x, self.y, self.z)
  

def add(a, b):
  return a + b

if name == &#39;main&#39;:
  add(p(1, 2), p(3, 4).info())
  add(D(1, 2, 3), D(1, 2, 3).info())
  add(F(2, 3, 4), D(2, 3, 4).info())
Copy after login

4. The benefits of polymorphism

< ;1> It can realize open expansion and closed modification

<2>Make python program more flexible

The above is the detailed content of Share examples of duck classes and polymorphism in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!