【1.101】静态属性 静态方法 类方法

静态属性 就是说  数据属性

1、静态  就是封装了函数  然类的函数属性 数据属性 化了

class Room:
    def __init__(self,name,owner,length,width,heighth):
        self.name = name
        self.owner = owner
        self.length = length
        self.width = width
        self.heighth = heighth
    def cal_area(self):
        return "%s住在%s,它的面积:%s"%(self.owner,self.name,self.length*self.width)

    def cal_volume(self):
        return "%s住在%s,它的体积:%s"%(self.owner,self.name,self.length*self.width*self.heighth)

r1 = Room("厕所","alex",100,100,100)
r2 = Room("公共厕所","yuhao",1,1,100)

print(r1.cal_area())
print(r2.cal_area())
print(r1.cal_volume())
print(r2.cal_volume())

# alex住在厕所,它的面积:10000
# yuhao住在公共厕所,它的面积:1
# alex住在厕所,它的体积:1000000
# yuhao住在公共厕所,它的体积:1
普通方法
class Room:
    def __init__(self,name,owner,length,width,heighth):
        self.name = name
        self.owner = owner
        self.length = length
        self.width = width
        self.heighth = heighth
    @property
    def cal_area(self):
        return "%s住在%s,它的面积:%s"%(self.owner,self.name,self.length*self.width)
    @property
    def cal_volume(self):
        return "%s住在%s,它的体积:%s"%(self.owner,self.name,self.length*self.width*self.heighth)

r1 = Room("厕所","alex",100,100,100)
r2 = Room("公共厕所","yuhao",1,1,100)



#print(r1.cal_area())  没有静态方法修饰

print(r1.cal_area)  #有静态方法修饰 ,但这里就没有括号了 就和调用数据属性一样了
#alex住在厕所,它的面积:10000
print(r1.name)        #厕所
#和上面property修饰了的函数一样,它一修饰就感觉将函数属性 数据属性化了



#print(r2.cal_volume()) 没有静态方法修饰

print(r2.cal_volume)  #有静态方法修饰 ,但这里就没有括号了 就和调用数据属性一样了
#yuhao住在公共厕所,它的体积:100
print(r1.owner)    #公共厕所
有静态方法修饰 ,被调用的函数不需要括号, 就和调用数据属性一样了,就是让调用的人不知道是函数还是数据,@property 它一修饰就感觉将函数属性 数据属性化了

2、类方法、静态方法

#property 和实例绑定
#classmethod  和类绑定
#staticmethod  和谁都不绑定
class Room:
    RoomNum = 1
    def __init__(self,name,owner,length,width,heighth):
        self.name = name
        self.owner = owner
        self.length = length
        self.width = width
        self.heighth = heighth

    @property
    def cal_area(self):

        return "%s住在%s,它的面积:%s"%(self.owner,self.name,self.length*self.width)
    @property
    def cal_volume(self):
        return "%s住在%s,它的体积:%s"%(self.owner,self.name,self.length*self.width*self.heighth)



    @classmethod   #只能类使用,实例也可以用  但是顾名思义就是类用的  实例用 cls 就传实例 实例加点调用RoomNum也是正常的
    def tell_info(cls):
        print("tell_info-oldgirl住的房间号:%s"%(cls.RoomNum))
    #这个 tell_info 是通过类方法来定义 使用  时 不用实例化  就可以通过类直接使用




    def tell(self):
        print("tell--oldgirl住的房间号:%s" % (self.RoomNum))
        # 这个 tell 是通过普通类方法 没有用classmethod修饰    使用  时 要进行实例化  才可以使用  类不能直接用

    @staticmethod  #相当于不传任何参数,直接用,就是类的工具包
    def developer(name):
        print("Room 的开发商是:%s"%(name))

    def test(x,y):
        print(x,y)
        #这个是没有意义的  没有和谁绑定 但是一调用就要传参数  就会报错 这个在面向对象第一节就讲过了




Room.developer("中国建筑")    #Room 的开发商是:中国建筑

r2 = Room("公共厕所","yuhao",1,1,1)

print(r2.developer("成都路桥"))
#Room 的开发商是:成都路桥
#None     函数没有返回值




Room.tell_info()  #实例化前就调用   tell_info-oldgirl住的房间号:1
r1 = Room("厕所","alex",100,100,100)
#实例化后才可以用
r1.tell()    #tell--oldgirl住的房间号:1



#print(r1.cal_area())  没有静态方法修饰
print(r1.cal_area)    # alex住在厕所,它的面积:10000  就是静态方法修饰了就可以象调用数据属性一样直接运行 不要括号
print(Room.RoomNum)   # 1
类方法 静态方法

 

posted @ 2016-05-10 00:01  科学小怪癖  阅读(93)  评论(0)    收藏  举报