zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    精英程序员
    C语言(2)
    C语言练习
    C语言工具---Code::Blocks
    C语言(1)
    【udacity】机器学习-神经网络
    【udacity】机器学习-回归
    【udacity】机器学习-决策树
    【术语记录】机器学习
    python入门(九):目录操作
  • 原文地址:https://www.cnblogs.com/anttech/p/12835621.html
Copyright © 2011-2022 走看看