概述
#接口思想:建立关联的桥梁
#开放封闭原则
#依赖导致原则
#接口隔离原则
#继承多态
#抽象类和接口类
#1.python中没有接口语法
import abc
'''
def jiao():pass
def chi():pass
def pao():pass
#知道操作功能,但不能明确操作的具体对象
print(len('123'))
#知道操作对象,不能明确具体操作方法
print('123'.__len__())
#接口类:用来定义功能的类,为继承它的子类提供功能的
#该接口的功能方法一不需要有实现体,实现体有继承他的子类自己去实现
class PetInterface:
def close_master(self):pass
class WatchInterface:
def watch_door(self):pass
class Dog(PetInterface,WatchInterface):
def jiao(self):pass
def chi(self):pass
def pao(self):pass
class Cat(PetInterface,WatchInterface):pass
class Applepay:
def payment(self, money):
print('苹果 支付了%d元' % money)
class Alipay:
def payment(self, money):
print('支付宝 支付了%d元' % money)
def payment(pay_obj, money):
pay_obj.payment(money)
apple = Applepay()
ali = Alipay()
payment(apple, 100)
payment(ali, 100)
import abc
class All_file(metaclass = abc.ABCMeta):
all_type = 'file'
@abc.abstractmethod
def read(self):
pass
@abc.abstractmethod
def write(self):
pass
class Txt(All_file):
def read(self):
print('文本数据的读取方式')
def write(self):
print('文本数据的写入方式')
class Sata(All_file):
def read(self):
print('硬盘读取方式')
def write(self):
print('硬盘写入方式')
wenben=Txt()
wenben.read()
yingpan = Sata()
yingpan.write()
print(wenben.all_type)
print(yingpan.all_type)
#抽象父类:拥有抽象方法(子类共有的方法,但是父类不能有具体的实现体)的父类
#抽象方法:方法名是具体的,但是实现体是抽象的(在子类中重写来具体化)
#python中借助abc来实现抽象父类
import abc
class Quan(metaclass = abc.ABCMeta):
def __init__(self,name):
self.name = name
def run (self):
print(self.name+'running')
@abc.abstractmethod
def chi(self):
pass
@abc.abstractmethod
def jiao(self):
pass
@classmethod
@abc.abstractmethod
def fn(cls):pass
class Dog(Quan):
@classmethod
def fn(cls):
print('fn gou running')
def kanmen(self):
print(self.name +'看门')
def chi(self):
super().chi()
print(self.name+'狗粮')
def jiao(self):
print('旺旺')
class Wolf(Quan):
@classmethod
def fn(cls):
print('fn worf running ')
def bulie(self):
print(self.name+'捕猎')
def chi(self):
print(self.name +'肉')
def jiao(self):
print('嗷嗷嗷')
dog =Dog('来福')
wolf = Wolf('常威')
dog.run()
dog.fn()
dog.chi()
dog.jiao()
wolf.fn()
dog.kanmen()
wolf.bulie()
#多态性
class People(metaclass = abc.ABCMeta):
def __init__(self,name):
self.name = name
@abc.abstractmethod
def speak(self):pass
class Chinese(People):
def speak(self):
print('speak chinese')
class England(People):
def speak(self):
print('speak english')
if __name__ == '__main__':
def ask_people(obj):
print('让%s上台演讲'%obj.name)
obj.speak()
ch = Chinese('张三')
en = England('tom')
ask_people(ch)
ask_people(en)
class People(metaclass = abc.ABCMeta):
def __init__(self,name):
self.name = name
@abc.abstractmethod
def speak(self):pass
class Chinese(People):
def sepak(self):
print('speak chinese')
class England(People):
def speak(self):
print('speak english')
#鸭子类型
#1 规定有什么属性以及什么方法的类的类型叫做鸭子类型
#2 能提供出规定的属性与方法的对象就是鸭子
class Test:
def __init__(self,name):
self.name = name
def speak(self):
print('说计算机语言')
if __name__ == '__main__':
def ask_people(obj):
print('让%s上台演讲'%obj.name)
obj.speak()
test=Test('测试者')
ask_people(test)
ls =[1,2,3,4,5]
print(ls,type(ls))
s = 'abc'
print(s,type(s))
class A:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return '<name:%s|age:%s>'%(self.name,self.age)
def __del__(self):
del self.name
a =A('tom ',18)
print(a,type(a))
class B:
def __setattr__(self, key, value):
b.__dict__[key] = value
def __setitem__(self, key, value):
self.__dict__[key] = value
b = B()
b.name = 'BBBBBB'
print(b.name)
b['age'] =18
print(b.age)
b.age = 18
print(b.age)
'''
#反射:通过字符串与类以及类的对象的属性(方法)建立关联
class A:
def __init__(self,name):
self.name = name
def t1(self):
print(self.name,'t1 working')
def t2(self):
print(self.name,'t2 working')
def t3(self):
print(self.name,'t3 working')
def t4(self):
print(self.name,'t4 working')
a = A('AAA')
m_map = {'t1':a.t1}
while True:
choice = input(':')
if choice in m_map:
m_map[choice]()
fn = getattr(A,choice,'no way')
print(fn)
fn(a)
break
转载于:https://www.cnblogs.com/guanlei/p/10750535.html
最后
以上就是现实帽子为你收集整理的多态与鸭子类型的全部内容,希望文章能够帮你解决多态与鸭子类型所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复