美文网首页
静态函数/类函数

静态函数/类函数

作者: 手捧樱花v | 来源:发表于2022-02-08 22:14 被阅读0次

    类函数实现不同的init构造函数

    class Document:
        WELCOME_STR = "welcomee! the context for this book is {}"
    
        def __init__(self,title,author,context):
            print("init function called")
            self.title = title
            self.author = author
            self.__context = context
    
        @classmethod
        def create_empty_book(cls,title,author):
            return cls(title=title,author=author,context='nothing')
    
        @classmethod
        def sakura(cls,title,author):
            return cls(title=title,author=author,context="sakura")
    
        def get_context_length(self):
            return self.__context
    
        @staticmethod
        def get_welcome(context):
            return Document.WELCOME_STR.format(context)
    
    empty_book = Document.create_empty_book('What every man thinks about aprt from sex','professor sheridan simove')
    print(empty_book.get_context_length())
    print(empty_book.get_welcome('indeed nothing'))
    
    saku = Document.sakura('sakura1','sakura2')
    print(saku.get_context_length())
    

    相关文章

      网友评论

          本文标题:静态函数/类函数

          本文链接:https://www.haomeiwen.com/subject/aeyikrtx.html