静态方法(可调类变量、可被实例调用、可被类调用)
1、用 @staticmethod 装饰的不带 self 参数的方法叫做静态方法,类的静态方法可以没有参数,可以直接使用类名调用
2、静态方法名义上归类管理,实际中在静态方法中无法访问类和实例中的任何属性
3、调用时并不需要传递类或者实例。像我们在类外定义的函数,只不过静态方法可以通过类或者实例来调用而已
#实验证明eat静态方法里不能传self对象,因此无法调用实例变量
import requests,json
class Dog(object):
food='牛肉'
name='大黄狗'
def __init__(self, name):
self.name = name
@staticmethod
def 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 <module>
d.eat()
TypeError: eat() missing 1 required positional argument: 'self'
Process finished with exit code 1
#实验证明eat静态方法,可由类和实例调用,可使用类变量
class Dog(object):
food='牛肉'
name='大黄狗'
def __init__(self, name):
self.name = name
@staticmethod
def 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 = name
@classmethod
def 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 = name
@classmethod
def 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 <module>
d.eat('橙子家')
File "C:/Users/wangli/PycharmProjects/Test/Test/test.py", line 102, in eat
print('%s %s eat %s' %(who,self.name,Dog.food))
AttributeError: type object 'Dog' has no attribute 'name'
Process 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 = name
def 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 = name
self.__food = None
@property
def eat(self):
print('%s eat %s' %(self.name,self.__food))
@eat.setter
def eat(self, food):
self.__food = food
d = Dog("labuladuo")
d.eat
d.eat = "baozi"
d.eat
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
wang eat None
wang eat baozi
Process finished with exit code 0
属性方法应用场景
比如 ,你想知道一个航班当前的状态,是到达了、延迟了、取消了、还是已经飞走了, 想知道这种状态你必须经历以下几步:
1. 连接航空公司API查询
2. 对查询结果进行解析
3. 返回结果给你的用户
因此这个status属性的值是一系列动作后才得到的结果,所以你每次调用时,其实它都要经过一系列的动作才返回你结果,但这些动作过程不需要用户关心, 用户只需要调用这个属性就可以
import requests,json
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):
print('fight %s has changed status to %s'%(self.flight_name,status))
f = Flight("CA980")
f.flight_status
f.flight_status=2
"C:\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 2
Process finished with exit code 0