python基础----多态与多态性、super函数用法、继承原理

时间:2023-03-09 18:41:57
python基础----多态与多态性、super函数用法、继承原理

一、多态与多态性                                                                       

㈠多态:

多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承)

1. 序列类型有多种形态:字符串,列表,元组。

2. 动物有多种形态:人,狗,猪

 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')

3. 文件有多种形态:文件文件,可执行文件

 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')

4多态:同一种事物的多种形态,动物分为人类,猪类(在定义角度)

 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()

㈡多态性:

请务必注意注意注意:多态与多态性是两种概念.

多态性:一种调用方式,不同的执行效果

多态性是指具有不同功能的函数可以使用相同的函数名,这样就可以用一个函数名调用不同内容的函数。

在面向对象方法中一般是这样表述多态性:向不同的对象发送同一条消息,不同的对象在接收时会产生不同的行为(即方法)。也就是说,每个对象可以用自己的方式去响应共同的消息。所谓消息,就是调用函数,不同的行为就是指不同的实现,即执行不同的函数。

多态性分为静态多态性和动态多态性

静态多态性:如任何类型都可以用运算符+进行运算

动态多态性:如下

1.

python基础----多态与多态性、super函数用法、继承原理

2.

>>> 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.

>>> def func(f):
... f.click()
...
>>> t1=Text()
>>> e1=ExeFile()
>>> func(t1)
open file
>>> func(e1)
execute file

4.

# 多态性依赖于:
# 1.继承
# 2.如下
##多态性:定义统一的接口,
def func(obj): #obj这个参数没有类型限制,可以传入不同类型的值
obj.run() #调用的逻辑都一样,执行的结果却不一样 func(peo1)
func(pig1) func(d1)

二、super函数用法                                                                    

分别在python2和python3当中的用法

在python2中:

 #coding:utf-8
#super在python2中的用法:
# 1:super(自己的类,self).父类的函数名字
# 2:super只能用于新式类
class People(object):
def __init__(self,name,sex,age):
self.name=name
self.age=age
self.sex=sex
def walk(self):
print('%s is walking' %self.name)
class Chinese(People):
country='China'
def __init__(self,name,sex,age,language='Chinese'):
# self.name=name
# self.sex=sex
# self.age=age
# People.__init__(self,name,sex,age)
super(Chinese,self).__init__(name,sex,age)
self.language=language
c=Chinese('egon','male',18)
print c.name,c.age,c.sex,c.language

在python3中:

 #super在python3中的用法:
class People:
def __init__(self,name,sex,age):
self.name=name
self.age=age
self.sex=sex
def walk(self):
print('%s is walking' %self.name)
class Chinese(People):
country='China'
def __init__(self,name,sex,age,language='Chinese'):
# self.name=name
# self.sex=sex
# self.age=age
# People.__init__(self,name,sex,age)
super(Chinese,self).__init__(name,sex,age)
self.language=language
def walk(self,x):
super().walk()
print('子类的x',x)
c=Chinese('egon','male',18)
# print(c.name,c.age,c.sex,c.language)
c.walk(123)

三、继承原理                                                                             

新式类的继承,在查找属性时遵循:广度优先 (python3中都是新式类)

python2中经典类的继承,在查找属性时遵循:深度优先

# coding:utf-8
# 新式类的继承,在查找属性时遵循:广度优先
class A(object):
def test(self):
print('from A')
pass
class B(A):
# def test(self):
# print('from B')
pass
class C(A):
# def test(self):
# print('from C')
pass
class D(B):
# def test(self):
# print('from D')
pass class E(C):
# def test(self):
# print('from E')
pass
class F(D,E):
# def test(self):
# print('from F')
pass
f1=F()
# f1.test() # print(F.__mro__) #只有新式类才有这个属性可以查看线性列表,经典类没有这个属性
print(F.mro()) # mro(method resolution order):方法解析顺序 # 广度优先:F->D->B->E->C->A->object #python2中经典类的继承,在查找属性时遵循:深度优先
class A:
# def test(self):
# print('from A')
pass
class B(A):
# def test(self):
# print('from B')
pass
class C(A):
# def test(self):
# print('from C')
pass
class D(B):
# def test(self):
# print('from D')
pass class E(C):
# def test(self):
# print('from E')
pass
class F(D,E):
# def test(self):
# print('from F')
pass
f1=F()
f1.test() # F->D->B->A->E->C

继承原理-例1

#新式类的继承,在查找属性时遵循:广度优先
class A(object):
def test(self):
print('from A')
pass
class X(A):
# def test(self):
# print('from X')
pass
class B(X):
# def test(self):
# print('from B')
pass
class C(A):
# def test(self):
# print('from C')
pass
class D(B):
# def test(self):
# print('from D')
pass class E(C):
# def test(self):
# print('from E')
pass
class F(D,E):
# def test(self):
# print('from F')
pass
f1=F()
f1.test() #广度优先:F->D->B->E->C->A->object

继承原理-例2

#新式类的继承,在查找属性时遵循:广度优先
class X(object):
# def test(self):
# print('from X')
pass
class Y(object):
# def test(self):
# print('from Y')
pass class B(X):
# def test(self):
# print('from B')
pass
class C(Y):
# def test(self):
# print('from C')
pass
class D(B):
# def test(self):
# print('from D')
pass class E(C):
# def test(self):
# print('from E')
pass
class F(D,E):
# def test(self):
# print('from F')
pass
f1=F()
f1.test() #F--->D---->B--->X--->E---->C---->Y---->object

继承原理-例3

#新式类的继承,在查找属性时遵循:广度优先
class A(object):
def test(self):
print('from A')
pass class B(A):
# def test(self):
# print('from B')
pass class C(A):
# def test(self):
# print('from C')
pass class D(A):
# def test(self):
# print('from D')
pass
class E(B):
# def test(self):
# print('from E')
pass
class F(C):
# def test(self):
# print('from F')
pass class G(D):
# def test(self):
# print('from G')
pass class H(E,F,G):
# def test(self):
# print('from H')
pass h1=H()
h1.test()

继承原理-例4

class A:
def fa(self):
print('from A')
def test(self):
self.fa()
class B(A):
def fa(self):
print('from B')
b=B()
b.test() #b.test--->B--->A b.fa()

继承原理-例5

灵魂画师(矮根儿)的杰作:  。◕‿◕。

python基础----多态与多态性、super函数用法、继承原理python基础----多态与多态性、super函数用法、继承原理

python基础----多态与多态性、super函数用法、继承原理python基础----多态与多态性、super函数用法、继承原理