属性的定义和调用

1,定义时,在普通方法的基础上添加@property装饰器

2,定义时,属性仅有一个self参数

3,调用时,无需括号

vim day7-8.py

#!/usr/bin/python
# -*- coding:utf-8 -*-
class Pager(): def __init__(self,current_page):
self.current_page = current_page #代表第几页
self.per_items = 10 #每页显示行数 @property
def start(self): #开始的序号
val = (self.current_page - 1) * self.per_items + 1
return val @property
def end(self): #结束序号
val = self.current_page * self.per_items
return val p = Pager(2)
print p.start
print p.end

使用以上方法可以根据页数取得需要查询的数据的序号开始和结束值

这里使用属性的方法是为了调用看起来更加合理

取消属性装饰器,调用的时候就要加()

所以以上代码和以下代码的效果的一样的

#!/usr/bin/python
# -*- coding:utf-8 -*-
class Pager(): def __init__(self,current_page):
self.current_page = current_page #代表第几页
self.per_items = 10 #每页显示行数 # @property
def start(self): #开始的序号
val = (self.current_page - 1) * self.per_items + 1
return val # @property
def end(self): #结束序号
val = self.current_page * self.per_items
return val p = Pager(2)
print p.start()
print p.end()

PS:属性在编程时可能用不上,知道即可

  属性有两种表达方式一种是加property装饰器一种是使用property方法

对于新式类不仅仅支持property还支持

vim day7-9.py

#!/usr/bin/python
# -*- coding:utf-8 -*-
class Goods(object): @property
def price(self):
print '@property' @price.setter
def price(self,value):
print '@property.setter' @price.deleter
def price(self):
print '@property.deleter' obj = Goods() obj.price #自动执行 @property修饰的price方法,并获取方法的返回值
obj.price = 123 #自动执行 @price.setter装饰的price方法,并将123赋值给方法
del obj.price #自动执行 @price.deleter装饰的price方法
~

PS:知道即可,很少用

最新文章

  1. AngularJS HTML DOM
  2. 一个解决chrome浏览器下input标签当autocomplete的时候背景变黄色同时input背景图片消失方案
  3. 使用Maven构建多模块项目
  4. Linux nfs配置
  5. Angularjs的My97DatePicker扩展
  6. PHP伪造referer突破防盗链
  7. SharePoint 2010 Modal Dialog
  8. TP5学习基础一:增删改查小demo
  9. day21双下方法,源码相关,异常处理
  10. 微信小程序代码快速转化为H5代码的方法!
  11. CSRedisCore 在net core中的使用
  12. Makefile中的%标记和系统通配符*的区别
  13. nginx-1.13.12 源码配置清单
  14. 转:Ubuntu 10.10 安装后上不了网的原因
  15. Python day5 --------递归、匿名函数、高阶函数、内置函数
  16. (转)SQL Server 2008无法修改表的解决办法
  17. Bootstrap-Plugin:附加导航(Affix)插件
  18. JS实现键盘监听(包括组合键)
  19. [原]Android Studio使用极光推送出现at cn.jpush.android.service.ServiceInterface.a(Unknown Source) 已解决
  20. 如何优化JAVA代码及提高执行效率

热门文章

  1. C# winform开发嵌套Chrome内核浏览器(WebKit.net)开发(一)
  2. SpringMVC由浅入深day02_4springmvc校验
  3. Echarts调整图表上下左右的间距,Echarts调整柱状图左右的间距
  4. SVN 快速入门
  5. Python中执行外部命令
  6. 在线电路编程 (ICP)
  7. 详解SQL中的GROUP BY语句
  8. Material Design系列第五篇——Working with Drawables
  9. 【MySQL】 Cannot load from mysql.proc. The table is probably corrupted
  10. IT零起步-CentOS6.4部署OpenVPN服务器