| 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() |
| 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'</module> |
| 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() |
| 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'</module> |
| 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 |
| 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() |
| 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</module> |
| 1 2 3 4 5 | d = Dog("ChenRonghua") d.eat 输出 ChenRonghua is eating |
| 1 2 3 | 1. 连接航空公司API查询 2. 对查询结果进行解析 3. 返回结果给你的用户 |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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 |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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 = { : "canceled", :"arrived", : "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 |
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |