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

静态函数/类函数

作者: 手捧樱花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())

相关文章

  • 静态类是不能实例化的

    静态类 原则 静态类中的所有成员必须是静态的。 静态构造函数 静态类可以有静态构造函数,静态构造函数不可继承。 静...

  • 静态函数虚函数

    静态函数 静态函数一般用于处理类内的静态成员 如何区分静态函数和一般的类内函数呢? 观察这个函数 看看它是否知道是...

  • 静态类&& 单例设计模式.

    静态构造函数: 一定是静态类才有静态构造函数吗? 错的 静态函数的特点: 1.静态函数没有修饰符修饰(...

  • C# 静态类,字符串的常见API

    静态构造函数: 1.一定是静态类才有静态构造函数吗?错的 静态构造函数的特点: 1.静态构造函数没有修饰符修饰(默...

  • 构造函数 非静态代码块 构造函数的执行顺序

    执行结果 父类--静态代码块子类--静态代码块父类--非静态代码块父类--构造函数子类--非静态代码块子类--构造函数

  • 静态构造函数,字符串的几种函数

    静态构造函数: 1.一定是静态类才有静态构造函数吗?错的 静态函数的特点: 1.静态函数没有修饰符修饰(默认修饰符...

  • 静态函数/类函数

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

  • Python---class

    Base 对于类内的变量,无论在类定义中还是类的创建函数中,实例函数需要使用,都要加self: 类函数,静态函数 ...

  • python 13面向对象

    构造函数 析构函数 私有 类方法 静态方法 属性方法

  • java类初始化顺序

    结论 基类静态代码块 子类静态代码块 基类代码块 基类构造函数 子类代码块 子类构造函数 验证代码

网友评论

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

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