以下方法为高级方法,一般来说没什么卵用

1、静态方法

  @staticmethod

  相当于把类内的函数从类内独立出来,只是名义上归类管,实际上不可以调用类内的变量和函数

通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法

 

 1 class Dog(object):
 2  
 3     def __init__(self,name):
 4         self.name = name
 5  
 6     @staticmethod #把eat方法变为静态方法
 7     def eat(self):
 8         print("%s is eating" % self.name)
 9  
10  
11  
12 d = Dog("ChenRonghua")
13 d.eat()

 

上面的调用会出以下错误,说是eat需要一个self参数,但调用时却没有传递,没错,当eat变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传给self了。

想让上面的代码可以正常工作有两种办法

1. 调用时主动传递实例本身给eat方法,即d.eat(d) 

2. 在eat方法中去掉self参数,但这也意味着,在eat中不能通过self.调用实例中的其它变量了

 1 class Dog(object):
 2 
 3     def __init__(self,name):
 4         self.name = name
 5 
 6     @staticmethod
 7     def eat():
 8         print(" is eating")
 9 
10 
11 
12 d = Dog("ChenRonghua")
13 d.eat()

2、类方法

类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量

  • 类属性 就是针对 类对象 定义的属性
    • 使用 赋值语句class 关键字下方可以定义 类属性
    • 类属性 用于记录 与这个类相关 的特征
  • 类方法 就是针对 类对象 定义的方法
    • 类方法 内部可以直接访问 类属性 或者调用其他的 类方法

语法如下

@classmethod
def 类方法名(cls):
    pass
  • 类方法需要用 修饰器 @classmethod 来标识,告诉解释器这是一个类方法
  • 类方法的 第一个参数 应该是 cls
    • 哪一个类 调用的方法,方法内的 cls 就是 哪一个类的引用
    • 这个参数和 实例方法 的第一个参数是 self 类似
    • 提示 使用其他名称也可以,不过习惯使用 cls
  • 通过 类名. 调用 类方法调用方法时,不需要传递 cls 参数
  • 在方法内部
    • 可以通过 cls. 访问类的属性
    • 也可以通过 cls. 调用其他的类方法

示例需求

  • 定义一个 工具类
  • 每件工具都有自己的 name
  • 需要 封装一个 show_tool_count 的类方法,输出使用当前这个类,创建的对象个数
@classmethod
def show_tool_count(cls):
    """显示工具对象的总数"""
    print("工具对象的总数 %d" % cls.count)

 

3、属性方法

属性方法的作用就是通过@property把一个方法变成一个静态属性

 

 1 class Dog(object):
 2  
 3     def __init__(self,name):
 4         self.name = name
 5  
 6     @property
 7     def eat(self):
 8         print(" %s is eating" %self.name)
 9  
10  
11 d = Dog("ChenRonghua")
12 d.eat()

 

调用会出以下错误, 说NoneType is not callable, 因为eat此时已经变成一个静态属性了, 不是方法了, 想调用已经不需要加()号了,直接d.eat就可以了

正常调用如下

 

1 d = Dog("ChenRonghua")
2 d.eat
3  
4 输出
5  ChenRonghua is eating

 

所以属性方法就是将一个方法(函数)变成一个静态属性(变量的调用形式)

赋值方法:

 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     @property
12     def flight_status(self):
13         status = self.checking_status()
14         if status == 0 :
15             print("flight got canceled...")
16         elif status == 1 :
17             print("flight is arrived...")
18         elif status == 2:
19             print("flight has departured already...")
20         else:
21             print("cannot confirm the flight status...,please check later")
22     
23     @flight_status.setter #修改
24     def flight_status(self,status):
25         status_dic = {
26 : "canceled",
27 :"arrived",
28 : "departured"
29         }
30         print("\033[31;1mHas changed the flight status to \033[0m",status_dic.get(status) )
31 
32     @flight_status.deleter  #删除
33     def flight_status(self):
34         print("status got removed...")
35 
36 f = Flight("CA980")
37 f.flight_status
38 f.flight_status =  2 #触发@flight_status.setter 
39 del f.flight_status #触发@flight_status.deleter
View Code

类的特殊成员方法:http://www.cnblogs.com/alex3714/articles/5213184.html

 

posted on 2018-07-12 19:45  一抹烟霞  阅读(186)  评论(0编辑  收藏  举报

Live2D