抽象类

python2中的写法

import abc
class Alert(object):
    '''报警基类'''
    __metaclass__ = abc.ABCMeta
    @abc.abstractmethod
    def send(self):
        '''报警消息发送接口'''
        pass

class MailAlert(Alert):
    pass

m = MailAlert()
m.send()

 

python3中的写法

class Alert(object):
    def send(self):
        raise NotImplementedError

class MailAlert(Alert):
    def send(self,msg):
        print('--sending--',msg)

class SMSAlert(Alert):
    pass

m = MailAlert()
m.send()

必须重构父类的send方法,否则主动抛出错误。

 

静态方法:不能访问公有属性,也不能访问实例

应用场景:实例多,节省内存开销

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

1、主动传实例给eat方法

class Person(object):
    def __init__(self,name):
        self.name = name
    @staticmethod
    def eat(self):
        print('%s is eating '%self.name)
p = Person('alex')
p.eat(p)

2、和类没什么关系,和函数差不多,只是通过类进行调用。

class Person(object):
    def __init__(self,name):
        self.name = name
    @staticmethod
    def eat(name):
        print('%s is eating '%name)

Person.eat('alex')

类方法:访问类的公有属性,不能访问实例属性

class Dog(object):
    name = "alex"

    def __init__(self, name):
        self.name = name

    @classmethod
    def eat(self):
        print("%s is eating" % self.name)


d = Dog("xxx")
d.eat()

输出结果是alex is eating,和实例化里的name没关系

 

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

class Dog(object):
 
    def __init__(self,name):
        self.name = name
 
    @property
    def eat(self):
        print(" %s is eating" %self.name)
 
 
d = Dog("alex")
d.eat

在调用eat时不能加(),此时eat是属性而不是方法

属性方法的应用场景:

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

1. 连接航空公司API查询

2. 对查询结果进行解析 

3. 返回结果给你的用户

因此这个status属性的值是一系列动作后才得到的结果,所以你每次调用时,其实它都要经过一系列的动作才返回你结果,但这些动作过程不需要用户关心, 用户只需要调用这个属性就可以,还是看代码吧。

class Flight(object):
    def __init__(self,name):
        self.flight_name = name


    def checking_status(self):
        print("checking flight %s status " % self.flight_name)
        return  1

    @property
    def flight_status(self):
        status = self.checking_status()
        if status == 0 :
            print("flight got canceled...")
        elif status == 1 :
            print("flight is arrived...")
        elif status == 2:
            print("flight has departured already...")
        else:
            print("cannot confirm the flight status...,please check later")


f = Flight("CA980")
f.flight_status

既然flight_status是属性了,给它改个值,改完之后就sb了

class Flight(object):
    def __init__(self,name):
        self.flight_name = name


    def checking_status(self):
        print("checking flight %s status " % self.flight_name)
        return  1


    @property
    def flight_status(self):
        status = self.checking_status()
        if status == 0 :
            print("flight got canceled...")
        elif status == 1 :
            print("flight is arrived...")
        elif status == 2:
            print("flight has departured already...")
        else:
            print("cannot confirm the flight status...,please check later")
    
    @flight_status.setter #修改
    def flight_status(self,status):
        status_dic = {
            0 : "canceled",
            1 :"arrived",
            2 : "departured"
        }
        print("\033[31;1mHas changed the flight status to \033[0m",status_dic.get(status) )

    @flight_status.deleter  #删除
    def flight_status(self):
        print("status got removed...")

f = Flight("CA980")
f.flight_status
f.flight_status =  2 #触发@flight_status.setter 
del f.flight_status #触发@flight_status.deleter

类的特殊成员方法

1.__doc__表示类的描述信息

class Foo:
    """ 描述类信息,这是用于看片的神奇 """
 
    def func(self):
        pass
 
print Foo.__doc__
#输出:类的描述信息

2. __module__ 和  __class__ 

  __module__ 表示当前操作的对象在那个模块

  __class__     表示当前操作的对象的类是什么

class Foo(object):
    """ 描述类信息,这是用于看片的神奇 """

    def func(self):
        pass

if __name__ == '__main__':
    print(Foo.__doc__)

相关文章:

  • 2022-01-17
  • 2022-01-19
  • 2021-06-06
  • 2021-08-11
  • 2021-12-25
  • 2022-02-04
  • 2021-10-22
猜你喜欢
  • 2021-11-21
  • 2022-02-03
  • 2021-12-05
  • 2021-09-08
  • 2022-12-23
  • 2022-01-31
  • 2022-02-22
相关资源
相似解决方案