【Python】 属性的 get 与 set 方法
2016-05-18 20:56
561 查看
在C#里面,属性的get 与 set 非常简单方便。
而在Python里面,属性可以直接获取或赋值。但是如果在获取或赋值时加一些逻辑判断,就稍微有点不一样。
不过总的比起来要比java好多了(没错,我就是java黑)。
public class bird { public int age { get;set; } public bool isadult { get { return this.age >= 1 ? true:false; } } }
而在Python里面,属性可以直接获取或赋值。但是如果在获取或赋值时加一些逻辑判断,就稍微有点不一样。
class bird(object): def getAge(self): if age < 1 :return 1 else : return age def setAge(self,value): if value > 2 : value = 2 self.age = value age = property(getAge,setAge)
不过总的比起来要比java好多了(没错,我就是java黑)。
相关文章推荐
- Python 调试工具 PDB
- 堆
- 简单百科爬虫 python
- 百度图片爬虫-python版-如何爬取百度图片?
- 使用Python生成随机简单的验证码
- 集合
- python中的除法和取模
- Python __init__.py 用法
- PythonOpenCV:MLP用于最近邻搜索
- python数据分析3:双色球 单个红和蓝球哪个比例高
- 曲奇饼问题
- Python笔记之入门(高级篇)
- Python之编码规范
- 6.python学习笔记:切分大型文件
- Python字符串、列表、字典、元组互转
- Python之开发环境的搭建
- Python之简介
- selenium+python笔记2
- python计算时间差的方法
- Python Tricks(七)—— 使用 list 仿真先入先出的队列(FIFOQueue)