静态函数/类函数
2022-02-08 本文已影响0人
手捧樱花v
类函数实现不同的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())