Python mixin 混入

直接上代码:app

Python Mixin(混入)这个功能,基本能够解决抽象类的定义问题,好比说Java 里定义抽象类的时候,须要定义一个抽象函数,Python若想实现这种抽象函数的话用Mixin就不用在写抽象函数了,直接调用就行ide

# -*- coding:utf-8 -*- 
# 2016/8/29
# mail:ybs.kakashi@gmail.com


class Coder():
    def __init__(self, coder_type):
        print coder_type
        self.getIde()
        self.writecode()
        print "-----" * 10

    def writecode(self):
        print "writing code happy with his ide!!!"


class CPluseCoder():
    def getIde(self):
        print "Use VS ide"


class PythonCoder():
    def getIde(self):
        print "Use PC ide"


class SuperCoder():
    def getIde(self):
        print "can Use TEXT and shit any other IDE"


class JavaCoder():
    def getIde(self):
        print "User Eclipse"


class BaseCoder():
    def getIde(self):
        print "asking which ide is the best! any where !!!"


coder_dict = {
    "SuperCoder": SuperCoder,
    "PythonCoder": PythonCoder,
    "CPluseCoder": CPluseCoder,
    "JavaCoder": JavaCoder,
    "BaseCoder": BaseCoder
}


def get_coder(coder_name=None):
    Coder.__bases__ = (coder_dict.get(coder_name, BaseCoder),)
    if coder_name is None:
        coder_name = "Finder"
    return Coder(coder_name)


get_coder("SuperCoder")
get_coder("PythonCoder")
get_coder("CPluseCoder")
get_coder("JavaCoder")
get_coder()

执行结果:函数

SuperCoder
can Use TEXT and shit any other IDE
writing code happy with his ide!!!
--------------------------------------------------
PythonCoder
Use PC ide
writing code happy with his ide!!!
--------------------------------------------------
CPluseCoder
Use VS ide
writing code happy with his ide!!!
--------------------------------------------------
JavaCoder
User Eclipse
writing code happy with his ide!!!
--------------------------------------------------
Finder
asking which ide is the best! any where !!!
writing code happy with his ide!!!
--------------------------------------------------

Process finished with exit code 0
相关文章
相关标签/搜索