标签:col elf tag ret inf 封装 面向 Owner 静态
""" 特性 property 类方法 classmethed 静态方法 staticmethed """
property
封装逻辑,让调用者感受不到逻辑
class Room(object): #property 特性 def __init__(self,name,owner,width,length,heigh): self.name = name self.owner=owner self.width = width self.length = length self.heigh = heigh def cal_area(self): print(self.width*self.length) @property def cal_areaw(self): return self.width*self.length n1 = Room(‘yy‘,‘wc‘,10,10,10) n1.cal_area() print (n1.cal_areaw) #100 print(n1.name) #yy
classmethed
class Room(object): #类方法 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 def cal_area(self): print(self.width*self.length) @classmethod def tell_info(cls): #cls 是 类名 print(cls) print(cls.tag) # n1 = Room(‘yy‘,‘wc‘,10,10,10) Room.tell_info() # 结果 # <class ‘__main__.Room‘> # 1
标签:col elf tag ret inf 封装 面向 Owner 静态
原文地址:https://www.cnblogs.com/augustyang/p/9083716.html