Python面向对象静态方法,类方法,属性方法

属性:

  公有属性 (属于类,每个类一份)

  普通属性 (属于对象,每个对象一份)

  私有属性 (属于对象,跟普通属性相似,只是不能通过对象直接访问)

方法:(按作用)

  构造方法

  析构函数

方法:(按类型)

  普通方法

  私有方法(方法前面加两个下划线)

  静态方法

  类方法

  属性方法

静态方法

@staticmethod

静态方法,通过类直接调用,不需要创建对象,不会隐式传递self

类方法

@classmethod

类方法,方法中的self是类本身,调用方法时传的值也必须是类的公有属性,

就是说类方法只能操作类本身的公有字段

class Dog(object):
food = "gutou"
age = "1"
def __init__(self, name):
self.NAME = name
@classmethod
def eat(self,age): #只能是类中的变量
# print(self.NAME)
print(age)
print(self.food) @classmethod
def eat1(self, age): # 只能是类中的变量
# print(self.NAME)
age = "2"
self.food = "tang"
@staticmethod
def print_1():
print(Dog.food, Dog.age) d = Dog("labuladuo")
d.eat(Dog.age) #通过对象调用
Dog.eat(Dog.age) #通过类调用
print("-----1-----")
d.eat1(Dog.age)
Dog.print_1()
print("--------2-------")
Dog.eat1(Dog.age)
Dog.print_1()

属性方法

属性变为私有属性,加断言

class Cycle(object):
def __init__(self,x,y,radius):
self.x = x
self.y = y
self.radius = radius
@property
def radius(self):
return self.__radius
@radius.setter
def radius(self,radius):
assert radius > 0, "radius must be nonzero and non-negative"
self.__radius = radius
@radius.deleter
def radius(self):
del self.__radius
def __str__(self):
return "({0},{1},{2})".format(self.x,self.y,self.radius) c = Cycle(1,1,7)
c.radius = 9
print(c)
del c.radius
print(c.radius)
#(1,1,9)
#AttributeError: 'Cycle' object has no attribute '_Cycle__radius'
class Dog(object):
def __init__(self, name):
self.name = name
self.__food = None
# def eat(self, food):原始方式
# self.__food = food
# print('%s eat %s' %(self.name, food))
@property
def eat(self):
print('%s eat %s' %(self.name,self.__food))
@eat.setter
def eat(self, food):
self.__food = food
@eat.deleter
def eat(self):
del self.__food
print("删完了") d = Dog("labuladuo")
# d.eat("baozi") #原始方式
d.eat
d.eat = "baozi"
d.eat #调用方式没变,只是改变传入的参数就改变了结果
#可以删除__food属性 del d.eat
d.eat # 报错 '''输出
labuladuo eat None
labuladuo eat baozi
删完了
AttributeError: 'Dog' object has no attribute '_Dog__food'
'''

属性方法应用场景

好吧,把一个方法变成静态属性有什么卵用呢?既然想要静态变量,那直接定义成一个静态变量不就得了么?well, 以后你会需到很多场景是不能简单通过 定义 静态属性来实现的, 比如 ,你想知道一个航班当前的状态,是到达了、延迟了、取消了、还是已经飞走了, 想知道这种状态你必须经历以下几步:

    1. 连接航空公司API查询
    1. 对查询结果进行解析
    1. 返回结果给你的用户

因此这个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
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

最新文章

  1. 【PostgreSQL】PostgreSQL添加新服务器连接时,报错“Server doesn't listen ”,已解决。
  2. sql count统计技巧
  3. iOS UIView 动画浅谈
  4. 网络服务器之HTTPS服务
  5. 【python】实用函数啥的
  6. ASP.NET MVC 学习之路由(URL Routing)
  7. 关于TCP连接建立与终止那点事
  8. 使用GIt向github上传代码
  9. python常见错误总结
  10. 《sed的流艺术之二》-linux命令五分钟系列之二十二
  11. [C#]Base使用小记
  12. Oracle的instr函数
  13. 1089. Insert or Merge (25)
  14. Visual Studio 2013创建自定义多项目模版
  15. (转)lwip TCP client & FreeRTOS 打开TCP 的 保活机制 LWIP_TCP_KEEPALIVE==1
  16. Python中使用operator模块实现对象的多级排序
  17. OkHttp3源码详解(一) Request类
  18. 数学战神app(小学生四则运算app)开发需求及进度
  19. dj cookie与session 2
  20. NodeJS旅程 : express - nodejs MVC 中的王牌

热门文章

  1. Centos6.8配置HTTPS
  2. SkylineGlobe 6.6 开放的事件函数接口
  3. 4《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)—目录
  4. CF101D Castle 树形DP、贪心
  5. 初始化应用程序数据ng-init指令
  6. 汇编 STOSB, STOSW, STOSD指令
  7. 《Head First 设计模式》例子的C++实现(2 观察者模式)
  8. HAProxy 日志输出及配置
  9. Linux ip forward
  10. Swarm基于多主机容器网络 - overlay networks 梳理