美文网首页
8.Python3类属性

8.Python3类属性

作者: 一枼落知天下 | 来源:发表于2019-05-31 17:46 被阅读0次
class Room:

    country = "中国"
    city = "成都"

    def __init__(self,name,owner):
        self.name = name
        self.owner = owner

    # 静态属性
    @property
    def detail(self):
        print("%s的主人:%s"%(self.name,self.owner))

    # 类方法,跟实例没有绑定关系,跟类绑定
    @classmethod
    def address(cls):
        print("%s玉林北路1号"%cls.city)

    # 静态方法,跟实例和类都没有绑定关系
    # 名义上归类管理,不能使用类属性和实例属性,
    # 只是类的工具包
    # 存在于类的属性或字典中
    # Room.__dict__
    @staticmethod
    def bathRoom():
        print("正在洗澡...")


if __name__ == '__main__':
    print(Room.__dict__)
    r1 = Room("帅府豪宅","JhouShuai")
    print(r1.__dict__)
    # r1.detail
    # Room.address()

相关文章

网友评论

      本文标题:8.Python3类属性

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