网站搭建谷歌seo,铜陵建设行业培训学校网站,超市网站建设方案,淘宝搜索词排名查询静态方法#xff08;可调类变量、可被实例调用、可被类调用#xff09;
1、用 staticmethod 装饰的不带 self 参数的方法叫做静态方法#xff0c;类的静态方法可以没有参数#xff0c;可以直接使用类名调用
2、静态方法名义上归类管理#xff0c;实际中在静态方法中无法…静态方法可调类变量、可被实例调用、可被类调用
1、用 staticmethod 装饰的不带 self 参数的方法叫做静态方法类的静态方法可以没有参数可以直接使用类名调用
2、静态方法名义上归类管理实际中在静态方法中无法访问类和实例中的任何属性
3、调用时并不需要传递类或者实例。像我们在类外定义的函数只不过静态方法可以通过类或者实例来调用而已
#实验证明eat静态方法里不能传self对象因此无法调用实例变量import requests,json
class Dog(object):food牛肉name大黄狗def __init__(self, name):self.name namestaticmethoddef eat(self):print(%s eat %s %(self.name,Dog.food))
d Dog(拉布拉多)
d.eat()
Dog.eat()C:\Program Files\Python35\python.exe C:/Users/wangli/PycharmProjects/Test/Test/test.py
Traceback (most recent call last):File C:/Users/wangli/PycharmProjects/Test/Test/test.py, line 104, in moduled.eat()
TypeError: eat() missing 1 required positional argument: selfProcess finished with exit code 1
#实验证明eat静态方法可由类和实例调用可使用类变量class Dog(object):food牛肉name大黄狗def __init__(self, name):self.name namestaticmethoddef eat(who):print(%s %s eat %s %(who,Dog.name,Dog.food))
d Dog(拉布拉多)
d.eat(橙子家)
Dog.eat(橘子家)C:\Program Files\Python35\python.exe C:/Users/wangli/PycharmProjects/Test/Test/test.py
橙子家 大黄狗 eat 牛肉
橘子家 大黄狗 eat 牛肉Process finished with exit code 0
类方法可调类变量、可被实例调用、可被类调用
1、类方法通过classmethod装饰器实现类方法和普通方法的区别是 类方法只能访问类变量不能访问实例变量
2、通过cls参数传递当前类对象不需要实例化直接通过类对象【实例名.方法名】和类对象实例【类名.方法名】访问 实验证明
通过cls参数传递当前类对象不需要实例化直接类名.方法名() 通过类对象和类对象实例访问import requests,json
class Dog(object):food牛肉name大黄狗def __init__(self, name):self.name nameclassmethoddef eat(cls,who):print(%s %s eat %s %(who,cls.name,Dog.food))
d Dog(大白狗)
d.eat(橙子家)
Dog.eat(橘子家)C:\Program Files\Python35\python.exe C:/Users/wangli/PycharmProjects/Test/Test/test.py
橙子家 大黄狗 eat 牛肉
橘子家 大黄狗 eat 牛肉Process finished with exit code 0
实验证明
类方法只能访问类变量不能访问实例变量class Dog(object):food牛肉#name大黄狗def __init__(self, name):self.name nameclassmethoddef eat(self,who):print(%s %s eat %s %(who,self.name,Dog.food))
d Dog(大白狗)
d.eat(橙子家)
Dog.eat(橘子家)C:\Program Files\Python35\python.exe C:/Users/wangli/PycharmProjects/Test/Test/test.py
Traceback (most recent call last):File C:/Users/wangli/PycharmProjects/Test/Test/test.py, line 104, in moduled.eat(橙子家)File C:/Users/wangli/PycharmProjects/Test/Test/test.py, line 102, in eatprint(%s %s eat %s %(who,self.name,Dog.food))
AttributeError: type object Dog has no attribute nameProcess finished with exit code 1
类实例方法可调类变量、可调实例变量、可被实例调用
1、第一个参数强制为类实例对象self可以通过这个类实例对象访问类属性self.name可以通过类实例对象的__class__属性访问类属性__class__.name。
2、类的初始化方法__init__也是实例方法在实例创建的时候自动调用
实验证明
实例方法可调用类变量和实例变量class Dog(object):food牛肉name大黄狗def __init__(self, name):self.name namedef eat(self,who): #实例方法print(%s %s eat %s %(who,self.name,Dog.food))
d Dog(大白狗)
d.eat(橙子家)C:\Program Files\Python35\python.exe C:/Users/wangli/PycharmProjects/Test/Test/test.py
橙子家 大白狗 eat 牛肉Process finished with exit code 0属性方法可调类变量、可调实例变量、可被实例调用
1、属性方法把一个方法变成静态属性可以调类的实例变量和类变量
给 属性方法赋值class Dog(object):def __init__(self, name):self.name nameself.__food Nonepropertydef eat(self):print(%s eat %s %(self.name,self.__food))eat.setterdef eat(self, food):self.__food food
d Dog(labuladuo)
d.eat
d.eat baozi
d.eatC:\Program Files\Python35\python.exe C:/Users/wangli/PycharmProjects/Test/Test/test.py
wang eat None
wang eat baoziProcess finished with exit code 0
属性方法应用场景
比如 你想知道一个航班当前的状态是到达了、延迟了、取消了、还是已经飞走了 想知道这种状态你必须经历以下几步:1. 连接航空公司API查询2. 对查询结果进行解析 3. 返回结果给你的用户因此这个status属性的值是一系列动作后才得到的结果所以你每次调用时其实它都要经过一系列的动作才返回你结果但这些动作过程不需要用户关心 用户只需要调用这个属性就可以import requests,json
class Flight(object):def __init__(self,name):self.flight_name namedef checking_status(self):print(checking flight %s status % self.flight_name)return 1propertydef 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.setterdef flight_status(self,status):print(fight %s has changed status to %s%(self.flight_name,status))
f Flight(CA980)
f.flight_status
f.flight_status2C:\Program Files\Python35\python.exe C:/Users/wangli/PycharmProjects/Test/Test/test.py
checking flight CA980 status
flight is arrived...
fight CA980 has changed status to 2Process finished with exit code 0