python之类的多态
多态:一种接口,多种实现
容许将子类类型的指针赋值给父类类型的指针。
作用:实现接口的重用
#Author:Anliu
# 多态本质上就是一个对象的多种形态。
# 不同的状态的描述需要抽象成类的多个子类,因而多态的概念依赖于继承
# 例如对于“文件”这个类来说,将有“文本文件”,“可执行文件”,“链接文件”,“设备文件”等等形态。
# 那如何给这些形态定义一个统一接口,就是多态干的事情。
class file():
    """
    定义一个父类
    """
    def __init__(self,name,handle):
        self.name = name
        self.handle = handle
    def storage(self):
        print("file of name: %s is storage in disk.."%self.name)
    @staticmethod         #定义统一接口的方法
    def file_open(obj):
        obj.open("name")
class text(file):  #文件的第一种状态
    def open(self,name):
        print("%s: text open file.."%name)
class exe(file):   #文件的第二种状态
    def open(self,name):
        print("%s: exe open file.."%name)
text1 = text("db","110011")
exe1 = exe("command","110001")
#def file_open(obj):
#    obj.open()
#text1.open()
file.file_open(text1)   #一种接口file.file_open()的多种状态
file.file_open(exe1) 相关推荐
  HongKongPython    2020-05-12  
   fly00love    2020-03-08  
   ladysosoli    2020-03-01  
   llwang0    2020-02-29  
   lynjay    2020-06-06  
   jameszgw    2020-06-03  
   Greatemperor    2020-05-17  
   Darklovy    2020-04-29  
   austindev    2020-04-25  
   sunnyJam    2020-04-15  
   TreasureZ    2020-02-22  
   jacktangj    2020-02-19  
   明天你好    2020-02-18  
   katyusha    2020-02-16  
   wangqing    2020-02-13  
   vanturman    2020-02-10  
   Winterto0    2020-01-28  
   chouliqingke    2020-01-13