@property@classmethod@staticmethod

一、静态属性
@property将方法标记成数据属性;可以访问实例和类的属性
 @classmethod标记成类的方法,不需要实例化,可以类直接调用的方法。可以访问类的属性方法,不能访问实例的

class Room:
    tag=1
    def __init__(self,name,owner,width,length,heigh):
        self.name=name
        self.owner=owner
        self.width=width
        self.length=length
        self.heigh=heigh

    @property
    def cal_area(self):
        # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
        return  self.width * self.length

    def test(self):
        print('from test',self.name)

    @classmethod
    def tell_info(cls,x):
        print(cls)
        print('--》',cls.tag,x)#print('--》',Room.tag)
    # def tell_info(self):
    #     print('---->',self.tag)


# print(Room.tag)

# Room.test(1) #1.name
# r1=Room('厕所','alex',100,100,100000)
Room.tell_info(10)

二、静态方法:

@staticmethod 类的工具包,跟类完全独立,不能调用类的属性 可以用实例和类直接调用
class Room:
    tag=1
    def __init__(self,name,owner,width,length,heigh):
        self.name=name
        self.owner=owner
        self.width=width
        self.length=length
        self.heigh=heigh

    @property
    def cal_area(self):
        # print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
        return  self.width * self.length

    @classmethod
    def tell_info(cls,x):
        print(cls)
        print('--》',cls.tag,x)#print('--》',Room.tag)
    # def tell_info(self):
    #     print('---->',self.tag)

    @staticmethod
    def wash_body(a,b,c):
        print('%s %s %s正在洗澡' %(a,b,c))

    def test(x,y):
        print(x,y)

# Room.wash_body('alex','yuanhao','wupeiqi')

print(Room.__dict__)


r1=Room('厕所','alex',100,100,100000)

print(r1.__dict__)
原文地址:https://www.cnblogs.com/jiawen010/p/10064083.html