我是靠谱客的博主 纯情心情,最近开发中收集的这篇文章主要介绍Python mixin 混入,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

直接上代码:

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

# -*- 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

 

转载于:https://my.oschina.net/u/2504425/blog/739396

最后

以上就是纯情心情为你收集整理的Python mixin 混入的全部内容,希望文章能够帮你解决Python mixin 混入所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部