什么是多态:
多态指的是一类事物有多种形态,比如动物有多种形态:人、狗、猪
class Animal: #同一类:动物
pass
class People(Animal): #形态一:人
pass
class Dog(Animal): #形态二:狗
pass
class Pig(Animal): #形态三:猪
pass
为何要有多态
# 多态性指的是可以在不考虑对象具体类型的情况下而直接使用对象
class Animal: # 统一所有子类的方法
def say(self):
print('动物基本的发声频率。。。',end=' ')
class People(Animal):
def say(self):
super().say()
print('嘤嘤嘤嘤嘤嘤嘤')
class Dog(Animal):
def say(self):
super().say()
print('汪汪汪')
class Pig(Animal):
def say(self):
super().say()
print('哼哼哼')
obj1=People()
obj2=Dog()
obj3=Pig()
obj1.say()
obj2.say()
obj3.say()
'''
动物基本的发声频率。。。 嘤嘤嘤嘤嘤嘤嘤
动物基本的发声频率。。。 汪汪汪
动物基本的发声频率。。。 哼哼哼
'''
更进一步:可以定义统一的接口,接收传入的动物对象
class Animal: # 统一所有子类的方法
def say(self):
print('动物基本的发声频率。。。',end=' ')
class People(Animal):
def say(self):
super().say()
print('嘤嘤嘤嘤嘤嘤嘤')
class Dog(Animal):
def say(self):
super().say()
print('汪汪汪')
class Pig(Animal):
def say(self):
super().say()
print('哼哼哼')
obj1=People()
obj2=Dog()
obj3=Pig()
# 定义统一的接口,接收传入的动物对象
def animal_say(animal):
animal.say()
animal_say(obj1)
animal_say(obj2)
animal_say(obj3)
'''
动物基本的发声频率。。。 嘤嘤嘤嘤嘤嘤嘤
动物基本的发声频率。。。 汪汪汪
动物基本的发声频率。。。 哼哼哼
'''
鸭子类型:
Python崇尚的“鸭子类型”(duck typing):“如果看起来像、叫声像而且走起路来像鸭子,那么它就是鸭子”。比起继承的方式,鸭子类型在某种程度上实现了程序的松耦合度,如下
class Cpu:
def read(self):
print('cpu read')
def write(self):
print('cpu write')
class Mem:
def read(self):
print('mem read')
def write(self):
print('mem write')
class Txt:
def read(self):
print('txt read')
def write(self):
print('txt write')
obj1=Cpu()
obj2=Mem()
obj3=Txt()
obj1.read()
obj1.write()
obj2.read()
obj2.write()
obj3.read()
obj3.write()
注:多态性的本质在于不同的类中定义有相同的方法名,这样我们就可以不考虑类而统一用一种方式去使用对象,可以通过在父类引入抽象类的概念来硬性限制子类必须有某些方法名
绑定方法与非绑定方法
类中定义的函数分为两大类:绑定方法和非绑定方法
其中绑定方法又分为绑定到对象的对象方法和绑定到类的类方法。
绑定方法:特殊之处在于将调用者本身当做第一个参数自动传入
绑定给对象的方法:调用者是对象,自动传入的是对象
绑定给类的方法:调用者类,自动传入的是类
在类中正常定义的函数默认是绑定到对象的,而为某个函数加上装饰器@classmethod后,该函数就绑定到了类。
# 配置文件settings.py的内容
HOST='127.0.0.1'
PORT=3306
# 类方法的应用
import settings
class Mysql:
def __init__(self,ip,port):
self.ip=ip
self.port=port
def func(self):
print('%s:%s' %(self.ip,self.port))
@classmethod # 将下面的函数装饰成绑定给类的方法
def from_conf(cls): # 从配置文件中读取配置进行初始化
# print(cls)
return cls(settings.IP, settings.PORT) #==>Mysql(settings.IP, settings.PORT)再传给init进行初始化
# obj1=Mysql('1.1.1.1',3306)
obj2=Mysql.from_conf() # 调用类方法,自动将类MySQL当作第一个参数传给cls
print(obj2.__dict__)
print(obj2.ip,obj2.port)
'''
{'ip': '127.0.0.0', 'port': 3306}
127.0.0.0 3306
'''
class Data_test2(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year
@classmethod
def get_date(cls,string_date):
#这里第一个参数是cls, 表示调用当前的类名
year,month,day=map(int,string_date.split('-'))
date1=cls(year,month,day)
#返回的是一个初始化后的类
return date1
def out_date(self):
print("year :",self.year)
print("month :",self.month)
print("day :",self.day)
r=Data_test2.get_date("2016-8-6")
r.out_date()
'''
year : 2016
month : 8
day : 6
'''
绑定到类的方法就是专门给类用的,但其实对象也可以调用,只不过自动传入的第一个参数仍然是类,也就是说这种调用是没有意义的,并且容易引起混淆
这也是Python的对象系统与其他面向对象语言对象系统的区别之一,比如Smalltalk和Ruby中,绑定到类的方法与绑定到对象的方法是严格区分开的。
非绑定方法:
为类中某个函数加上装饰器@staticmethod后,该函数就变成了非绑定方法,也称为静态方法。
该方法不与类或对象绑定,类与对象都可以来调用它,但它就是一个普通函数而已,因而没有自动传值那么一说
#没有绑定给任何人:调用者可以是类、对象,没有自动传参的效果
class Mysql:
def __init__(self,ip,port):
self.nid=self.create_id()
self.ip=ip
self.port=port
@staticmethod # 将下述函数装饰成一个静态方法
def create_id():
import uuid
return uuid.uuid4()
obj1=Mysql('1.1.1.1',3306)
# 静态方法,对于类或者对象来说调用都是函数
# print(Mysql.create_id) #
# print(obj1.create_id) #
# Mysql.create_id(1,2,3) #通过类或对象就是函数,所以create_id函数需要有几个参数就必须传几个参数
# obj1.create_id(4,5,6)
print(Mysql.create_id())
总结绑定方法与非绑定方法的使用:
若类中需要一个功能,该功能的实现代码是给对象使用则将其定义成对象方法、是给类使用则将其定义成类方法、无需引用类或对象则将其定义成静态方法。