day7-静态方法、类方法、属性方法

一、静态方法

 1 # coding:utf-8
 2 class Dog(object):
 3  
 4     def __init__(self,name):
 5         self.name = name
 6  
 7     # 静态方法:在方法前加上staticmethod装饰器
 8     # 使用场景:工具包各类封装,如os模块,封装没什么关系的system和mkdir
 9     # 此时需用类直接调用,不可以直接传入self, 否则会报错
10     @staticmethod  
11     def eat(food):
12         print("dog is eating {0}".format(food))
13 
14     # 名义上归类管理,实际上并访问不了实例中的任何属性
15     # 如果想访问,需传入实例本身
16     @staticmethod  
17     def eat2(self,food):
18         print("{0} is eating {1}".format(self.name,food))
19 
20 
21 print('-'*30)
22 Dog.eat("baozi")
23 
24 print('-'*30)
25 d = Dog("lulu")
26 d.eat2(d, "baozi")
27 
28 >>> 
29 ------------------------------
30 dog is eating baozi
31 ------------------------------
32 lulu is eating baozi

二、类方法

 1 # coding:utf-8
 2 class Dog(object):
 3  
 4     # 定义静态属性
 5     name = "xiaoxiao" 
 6     def __init__(self,name):
 7         self.name = name
 8  
 9     # 定义类方法
10     # 只能访问静态属性,不能访问实例变量
11     # 使用场景:写死变量,不容许更改,如朝鲜国籍
12     @classmethod  
13     def eat(self,food):
14         print("{0} is eating {1}".format(self.name,food))
15 
16 
17 print('-'*30)
18 d = Dog("lulu")
19 d.eat("baozi")
20 
21 >>> 
22 ------------------------------
23 xiaoxiao is eating baozi

三、属性方法

 1 # coding:utf-8
 2 class Dog(object):
 3  
 4     def __init__(self,name):
 5         self.name = name
 6         # 为了eat2的传参定义一个私有属性food
 7         self.__food = None
 8  
 9     # 定义属性方法
10     # 把一个方法转化为一个属性
11     # 使用场景,不需要关心动作过程,只需要调用属性获取结果,如航班状态
12     @property  
13     def eat(self):
14         print("{0} is eating".format(self.name))
15 
16 
17     @property
18     def eat2(self):
19         print("{0} is eating {1}".format(self.name, self.__food))
20  
21     # 上述的eat没办法传参,如果想传参,参照eat2
22     # 用@静态方法名.setter装饰方法, 来给转换后的静态属性赋值
23     @eat2.setter  
24     def eat2(self,food):
25         print("set to food:",food)
26         # 把参数赋给私有变量
27         self.__food = food
28 
29     # 如果直接使用del.eat2删除会报错,该属性不能删除, 此时需设置静态方法的删除属性
30     # 用@静态方法名.deleter装饰方法, 可以调用一些删除操作
31     @eat2.deleter   
32     def eat2(self):
33         del self.__food
34         print("food deleted")
35         
36 print('-'*30)
37 d = Dog('lulu')
38 # 调用不传值的属性方法
39 d.eat
40 
41 # 调用传值的属性方法
42 d.eat2 = 'baozi'
43 d.eat2
44 
45 del d.eat2
46 # 此时已经删除私有属性food,再调用会出错
47 # d.eat2
48 
49 ------------------------------
50 lulu is eating
51 ('set to food:', 'baozi')
52 lulu is eating baozi
53 food deleted

使用场景举例

你想知道一个航班当前的状态,是到达了、延迟了、取消了、还是已经飞走了, 想知道这种状态你必须经历以下几步:

1. 连接航空公司API查询

2. 对查询结果进行解析 

3. 返回结果给用户

status属性的值是一系列动作后才得到的结果,每次调用时都要经过一系列的动作才返回结果,但这些动作过程不需要用户关心, 用户只需要调用这个属性获取结果

 1 class Flight(object):
 2     def __init__(self,name):
 3         self.flight_name = name
 4  
 5  
 6     def checking_status(self):
 7         print("checking flight %s status " % self.flight_name)
 8         return  1
 9  
10  
11     # 定义航班状态的属性方法
12     @property
13     def flight_status(self):
14         # 检查航班状态
15         status = self.checking_status()
16         if status == 0 :
17             print("flight got canceled...")
18         elif status == 1 :
19             print("flight is arrived...")
20         elif status == 2:
21             print("flight has departured already...")
22         else:
23             print("cannot confirm the flight status...,please check later")
24      
25     # 设置属性方法可传参修改状态
26     @flight_status.setter 
27     def flight_status(self,status):
28         status_dic = {
29             0 : "canceled",
30             1 :"arrived",
31             2 : "departured"
32         }
33         print("Has changed the flight status to ",status_dic.get(status) )
34  
35     # 设置属性方法做些删除操作
36     @flight_status.deleter
37     def flight_status(self):
38         print("status got removed...")
39  
40 f = Flight("CA980")
41 f.flight_status
42 # 触发@flight_status.setter,修改航班状态
43 f.flight_status = 2
44 # 触发@flight_status.deleter 
45 del f.flight_status 
46 
47 >>> 
48 checking flight CA980 status 
49 flight is arrived...
50 ('Has changed the flight status to ', 'departured')
51 status got removed...

 

posted @ 2017-06-30 15:13  不知所以  阅读(97)  评论(0)    收藏  举报