class Room:
tag = 1
def __init__(self,owner,name,width,length):
self.Owner = owner
self.Name = name
self.Width = width
self.Length = length
@property #静态属性,把一个函数封装成类的数据属性
def cal_area(self):
#print("%s住的%s的面积%s" %(self.Name,self.Owner,self.Width*self.Length))
return self.Name,self.Owner,self.Width*self.Length
@classmethod #类方法,将实例和类分开,调用类时,不用先实例化,再传参数self
def tell_info(cls): #cls 代指类本身
print(cls)
print(cls.tag)
@staticmethod #静态方法,类的工具包,不和类与实例捆绑,不能使用类变量和实例变量
def eat_food(a,b):
print("%s and %s eating apples" %(a,b))
r1 = Room("客厅","alex",20,30)
r2 = Room("卧室","oct",25,25)
#r1.cal_area #当天把类中的函数做成一个property,则调用时,只调用属性,不用把调用的函数运行
print(r1.cal_area) #当return一个返回值时,用调用数据属性形式调用这个函数
Room.tell_info() #类方法自动传参
Room.eat_food("cot","sept")
r1.eat_food("cot","sept")
静态属性:将函数属性转换成类似数据属性的调用方法,类和实例属性都能调用
类方法:将类和实例分开,使得类只能调用类数据属性和函数属性,不能调用实例属性
静态方法:类的工具包,不和类与实例捆绑,不能调用类和实例属性