码迷,mamicode.com
首页 > 其他好文 > 详细

类的属性方法

时间:2016-11-28 08:06:07      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:str   访问   不能   mic   property   res   静态方法   装饰器   sof   

静态方法:

@staticmethod 装饰器可以把一个方法变成静态方法。 静态方法既不能访问公有属性,也不能访问实例属性 .这样的话,其实跟类已经没什么关系了。它与类唯一的关联就是需要通过类名来调用这个方法

 1 class Perison(object):
 2    
 3     def __init__(self,name):
 4         self.name = name
 5 
 6     @staticmethod        #静态方法:既不能访问公有属性,也不能访问实例属性
 7     def eat(name,food):
 8         print("静态方法 %s %s" %(name,food))
 9 
10 a= Perison(zcq)
11 a.eat(rch,apple)
12 >>
13 静态方法 rch apple

类方法:

@classmethod 装饰器,只能访问类的公有属性,不能访问实例属性

 

1 class Perison(object):
2     name = 我是类变量,我在顶部
3     def __init__(self,name):
4         self.name = name
5     @classmethod   # 类方法:只能访问类的公有属性,不能访问实例属性
6     def walk(self):
7         print("类方法",self.name)

a= Perison(‘zcq‘)
8 >>a.walk() 9 类方法 我是类变量,在我顶部

 

属性方法:

@property 装饰器,把一个方法变成一个静态属性,个人理解也就是把方法变成一个变量

 

 1 class Perison(object):
 2     def __init__(self,name):
 3         self.name = name
 4     @property        #属性方法的作用把一个方法变成一个静态属性(也就是把方法变成一个变量)
 5     def talk(self):
 6         print("属性方法 %s" %self.name)
 7 a= Perison(zcq)
 8 a.talk  
 9 >>
10 属性方法 zcq

这个属性的方法使用如下

 1 class Persion(object):
 2     ‘‘‘描述方法‘‘‘
 3     def __init__(self,name):
 4         self.name = name
 5         #self.__status = None
 6     def check_status(self):
 7         print(航班状态  %s %(self.name))
 8         return 1
 9     @property
10     def flight_status(self):
11         status = self.check_status()
12         if status == 0:
13             print(0000)
14         elif status == 1:
15             print(1111)
16         else:
17             print(----------------)
18     @flight_status.setter
19     def flight_status(self,status):
20         self.__status = status
21         print(用setter装饰器修改flight_status返回值)
22         print(self_status值为:,self.__status)
23 f=Persion(AC98)
24 f.flight_status
25 f.flight_status =10     #下面解释这里
26 >>
27 航班状态  AC98
28 状态为1
29 用setter装饰器修改flight_status返回值
30 self_status值为: 10
f.flight_status =10 解释:因为@property 是把一个方法变成一个静态属性,所以这也就可以理解为变量的赋值   这段赋值是因为下面又写了个flight_status 函数 这函数用了装饰器@flight_status.setter,所以就会打印这函数下定义的值

类的特殊成员方法:

各种类方法使用

1 class Foo:
2     """ 备注信息 """
3  
4     def func(self):
5         pass
6  
7 print(Foo.__doc__)
8 >>
9 备注信息
 1 class Persion(object):
 2     ‘‘‘描述方法‘‘‘
 3     def __init__(self,name):
 4         self.name = name
 5     def __call__(self, *args, **kwargs):
 6         print("__call",args,kwargs)
 7     def __str__(self):
 8         return <fligh:%s,status:%s> %(self.flight_status,self.__status)
 9     def __getitem__(self, item):
10         print(get item,item)
11         return 22
12     def __setitem__(self, key, value):
13         print(set key,key,value)
14     def __delitem__(self, key):
15         print(dele ,key)
16 f=Persion(AC98)
17 result = f[k1]  #自动触发执行 __getitem__
18 f[k2] = zcq   # 自动触发执行 __setitem__
19 print(f[age])   #打印getitem  return返回的值
20 >>
21 get item k1
22 set key k2 zcq
23 get item age
24 22

 

 

类的属性方法

标签:str   访问   不能   mic   property   res   静态方法   装饰器   sof   

原文地址:http://www.cnblogs.com/zcqdream/p/6107912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!