标签:类的属性 python基础 room 面向 def turn font first 东京
未使用静态属性之前:
class Room:
    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(‘%s 住的 %s 总面积是%s‘ % (self.owner,self.name, self.width * self.length))
        # return  self.width * self.length
r1 = Room("room01","小二",10,10,10)
r1.cal_area()  #小二 住的 room01 总面积是100@property ==>使用静态属性:
class Room:
    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)) #TypeError: ‘NoneType‘ object is not callable
        return  self.width * self.length
r1 = Room("room01","小二",10,10,10)
print(r1.cal_area)  #100  ==>调用方式发生了改变
print(r1.width)   #10
当我们不想进行实例化,而需要直接获取类的属性时,可以使用类方法@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)  #1
Room.tell_info(10)   #--》 1 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)
print(Room.__dict__)   #==>‘wash_body‘: <staticmethod object at 0x000002BFB0BB4630>
r1=Room(‘room12‘,‘alex‘,100,100,100000)
print(r1.__dict__)    #{‘width‘: 100, ‘heigh‘: 100000, ‘length‘: 100, ‘name‘: ‘room12‘, ‘owner‘: ‘alex‘}
定义一个人的类,人有头,躯干,手,脚等数据属性,这几个属性有可以是通过一个类实例化的对象,这就是组合
组合的用途:①做关联②小的组成大的
class Hand:
    pass
class Foot:
    pass
class Trunk:
    pass
class Head:
    pass
class Person:
    def __init__(self,id_num,name):
        self.id_num=id_num
        self.name=name
        self.hand=Hand()
        self.foot=Foot()
        self.trunk=Trunk()
        self.head=Head()
p1=Person(‘111111‘,‘AAA‘)
print(p1.__dict__)
#{‘name‘: ‘AAA‘, ‘foot‘: <__main__.Foot object at 0x00000273D44547B8>, ‘id_num‘: ‘111111‘, ‘trunk‘: <__main__.Trunk object at 0x00000273D44547F0>, ‘hand‘: <__main__.Hand object at 0x00000273D4454780>, ‘head‘: <__main__.Head object at 0x00000273D4454828>}
不建议使用的方式:
class School:
    def __init__(self,name,addr):
        self.name=name
        self.addr=addr
        self.course_list=[]   #定义列表
    def zhao_sheng(self):
        print(‘%s 正在招生‘ %self.name)
class Course:
    def __init__(self,name,price,period):
        self.name=name
        self.price=price
        self.period=period
s1=School(‘oldboy‘,‘北京‘)
s2=School(‘oldboy‘,‘南京‘)
s3=School(‘oldboy‘,‘东京‘)
c1=Course(‘linux‘,10,‘1h‘)
c2=Course(‘python‘,10,‘1h‘)
s1.course_list.append(c1)
s1.course_list.append(c2)
print(s1.__dict__)
for course_obj in s1.course_list:
    print(course_obj.name,course_obj.price)标签:类的属性 python基础 room 面向 def turn font first 东京
原文地址:https://www.cnblogs.com/hujinzhong/p/11487391.html