面向对象高级语法部分
经典类vs新式类
把下面代码用python2 和python3都执行一下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#_*_coding:utf-8_*_class A:
def __init__(self):
self.n = 'A'
class B(A):
# def __init__(self):
# self.n = 'B'
pass
class C(A):
def __init__(self):
self.n = 'C'
class D(B,C):
# def __init__(self):
# self.n = 'D'
pass
obj = D()
print(obj.n)
|
classical vs new style:
- 经典类:深度优先
- 新式类:广度优先
- super()用法
抽象接口
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import abc
class Alert(object):
'''报警基类'''
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def send(self):
'''报警消息发送接口'''
pass
class MailAlert(Alert):
pass
m = MailAlert()
m.send() |
上面的代码仅在py2里有效,python3里怎么实现呢?
-----------------------------------------
@staticmethod
#静态方法 实际和类没有什么关系了,调用不了类的变量也调用不了实例的变量。
#调用必须通过类调用。
@classmethod
#类方法 只能访问类变量,不能访问实例变量
@property
#属性方法 把一个方法变成一个静态属性 调用是不用()
如果要赋值:
class Dog(object):
def __init__():
self.__food = None
@property
def eat(self):
print("%s is eating" % self.__food)
@eat.setter #赋值调用的这个
def eat(self,food)
print("set to food:",food)
self.__food = food
@eat.deleter #删除调用这个
del eat(slef):
del self.__food
赋值:
d = Dog("lalala")
d.eat = "baozi"
d.eat
删除:
del d.eat
----------------------------------------
静态方法
通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Dog(object):
def __init__(self,name):
self.name = name
@staticmethod #把eat方法变为静态方法
def eat(self):
print("%s is eating" % self.name)
d = Dog("ChenRonghua")
d.eat() |
上面的调用会出以下错误,说是eat需要一个self参数,但调用时却没有传递,没错,当eat变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传给self了。
|
1
2
3
4
|
Traceback (most recent call last): File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/静态方法.py", line 17, in <module>
d.eat()
TypeError: eat() missing 1 required positional argument: '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()
类方法
类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class Dog(object):
def __init__(self,name):
self.name = name
@classmethod
def eat(self):
print("%s is eating" % self.name)
d = Dog("ChenRonghua")
d.eat() |
执行报错如下,说Dog没有name属性,因为name是个实例变量,类方法是不能访问实例变量的
|
1
2
3
4
5
6
|
Traceback (most recent call last): File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/类方法.py", line 16, in <module>
d.eat()
File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/类方法.py", line 11, in eat
print("%s is eating" % self.name)
AttributeError: type object 'Dog' has no attribute 'name'
|
此时可以定义一个类变量,也叫name,看下执行效果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Dog(object):
name = "我是类变量"
def __init__(self,name):
self.name = name
@classmethod
def eat(self):
print("%s is eating" % self.name)
d = Dog("ChenRonghua")
d.eat()#执行结果我是类变量 is eating
|
属性方法
属性方法的作用就是通过@property把一个方法变成一个静态属性
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class Dog(object):
def __init__(self,name):
self.name = name
@property
def eat(self):
print(" %s is eating" %self.name)
d = Dog("ChenRonghua")
d.eat() |
调用会出以下错误, 说NoneType is not callable, 因为eat此时已经变成一个静态属性了, 不是方法了, 想调用已经不需要加()号了,直接d.eat就可以了
|
1
2
3
4
5
|
Traceback (most recent call last): ChenRonghua is eating
File "/Users/jieli/PycharmProjects/python基础/自动化day7面向对象高级/属性方法.py", line 16, in <module>
d.eat()
TypeError: 'NoneType' object is not callable
|
正常调用如下
|
1
2
3
4
5
|
d = Dog("ChenRonghua")
d.eat输出 ChenRonghua is eating
|
好吧,把一个方法变成静态属性有什么卵用呢?既然想要静态变量,那直接定义成一个静态变量不就得了么?well, 以后你会需到很多场景是不能简单通过 定义 静态属性来实现的, 比如 ,你想知道一个航班当前的状态,是到达了、延迟了、取消了、还是已经飞走了, 想知道这种状态你必须经历以下几步:
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 @property 11 def flight_status(self): 12 status = self.checking_status() 13 if status == 0 : 14 print("flight got canceled...") 15 elif status == 1 : 16 print("flight is arrived...") 17 elif status == 2: 18 print("flight has departured already...") 19 else: 20 print("cannot confirm the flight status...,please check later") 21 22 23 f = Flight("CA980") 24 f.flight_status