Many people like to confuse polymorphism with polymorphism, and then think about the solution, in fact, as long as the separate look, it will be very clear.
One polymorphic:
Polymorphism refers to a class of things that have many forms, (an abstract class has more than one subclass, so the concept of polymorphism relies on inheritance)
- There are several patterns of sequence types: string, list, tuple
- Animals have many forms: man, dog, pig
1234567891011121314151617181920212223242526 |
#多态:同一种事物的多种形态,动物分为人类,猪类(在定义角度)
class
Animal:
def run(
self
):
raise
AttributeError(
‘子类必须实现这个方法‘
)
class
People(Animal):
def
run(
self
):
print
(
‘人正在走‘
)
class
Pig(Animal):
def
run(
self
):
print
(
‘pig is walking‘
)
class
Dog(Animal):
def
run(
self
):
print
(
‘dog is running‘
)
peo1
=
People()
pig1
=
Pig()
d1
=
Dog()
peo1.run()
pig1.run()
d1.run()
|
1234567891011121314151617 |
import
abc
class
Animal(metaclass
=
abc.ABCMeta):
#同一类事物:动物
@abc
.abstractmethod
def
talk(
self
):
pass
class
People(Animal):
#动物的形态之一:人
def
talk(
self
):
print
(
‘say hello‘
)
class
Dog(Animal):
#动物的形态之二:狗
def
talk(
self
):
print
(
‘say wangwang‘
)
class
Pig(Animal):
#动物的形态之三:猪
def
talk(
self
):
print
(
‘say aoao‘
)
|
There are several forms of files: files, text files, executable files
12345678910111213 |
import
abc
class
File
(metaclass
=
abc.ABCMeta):
#同一类事物:文件
@abc
.abstractmethod
def
click(
self
):
pass
class
Text(
File
):
#文件的形态之一:文本文件
def
click(
self
):
print
(
‘open file‘
)
class
ExeFile(
File
):
#文件的形态之二:可执行文件
def
click(
self
):
print
(
‘execute file‘
)
|
Two polymorphism
(1) What is polymorphism (note: Polymorphism and polymorphism are two concepts)
Polymorphism refers to functions with different functions that can use the same function name, so that functions with different contents can be called with a function name. In an object-oriented approach, it is common to express polymorphism: sending the same message to different objects, and different objects having different behavior (that is, methods) when they are received. In other words, each object can respond to a common message in its own way. The so-called message, is called the function, the different behavior refers to the different implementation, namely executes the different function.
12345678910111213141516171819202122 |
#多态性:一种调用方式,不同的执行效果(多态性)
def
func(obj):
obj.run()
func(peo1)
func(pig1)
func(d1)
# peo1.run()
# pig1.run()
# 多态性依赖于:继承
##多态性:定义统一的接口,
def
func(obj):
#obj这个参数没有类型限制,可以传入不同类型的值
obj.run()
#调用的逻辑都一样,执行的结果却不一样
func(peo1)
func(pig1)
func(d1)
|
1.
2.
123456789101112 |
>>>
def
func(animal):
#参数animal就是对态性的体现
... animal.talk()
...
>>> people1
=
People()
#产生一个人的对象
>>> pig1
=
Pig()
#产生一个猪的对象
>>> dog1
=
Dog()
#产生一个狗的对象
>>> func(people1)
say hello
>>> func(pig1)
say aoao
>>> func(dog1)
say wangwang
|
3.
123456789 |
>>> def func (f): ... F.click () ... >>> T1 = text () >>> E1 = exefile () >>> func (T1) open file >>> func (E1) execute file |
It can be said that polymorphism is an interface (function Func), a variety of implementations (such as F.click ())
Second, why to use polymorphism (polymorphism of the benefits)
In fact, we can see from the above polymorphism example, we do not add the above new knowledge, that is, Python itself is to support polymorphism, so what is the advantage of doing so?
(1) Increased flexibility of the program
Status quo, regardless of the ever-changing object, the user is the same form to invoke, such as func (animal)
(2) Increased scalability of the program
By inheriting the animal class, a new class is created, and the user does not have to change their own code or use Func (animal) to invoke the
1234567891011121314 |
>>>
class
Cat(Animal):
#属于动物的另外一种形态:猫
...
def
talk(
self
):
...
print
(
‘say miao‘
)
...
>>>
def
func(animal):
#对于使用者来说,自己的代码根本无需改动
... animal.talk()
...
>>> cat1
=
Cat()
#实例出一只猫
>>> func(cat1)
#甚至连调用方式也无需改变,就能调用猫的talk功能
say miao
‘‘‘
这样我们新增了一个形态Cat,由Cat类产生的实例cat1,使用者可以在完全不需要修改自己代码的情况下。使用和人、狗、猪一样的方式调用cat1的talk方法,即func(cat1)
‘‘‘
|
Note:
Polymorphism: Multiple forms of the same thing, animals divided into humans, pigs (in the definition of angle) Polymorphism: A method of invocation, different execution effect (polymorphism)
Polymorphism and polymorphism like Python basics