python Class:面向对象高级编程 @property
2018-07-19 16:25
811 查看
看不懂,先记录
一、
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Student(object):
@property
def score(self):
return self._score
@score.setter #setter是哪里来的??作用呢???
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value
s = Student()
s.score = 60
print s.score
运行结果:60
二、
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Student1(object):
@property #????
def birth(self):
return self._birth
@birth.setter #????
def birth(self, value):
self._birth = value
@property #????
def age(self):
return 2014 - self._birth
s = Student1()
s.birth = 60
print s.age
运行结果:60
一、
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Student(object):
@property
def score(self):
return self._score
@score.setter #setter是哪里来的??作用呢???
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value
s = Student()
s.score = 60
print s.score
运行结果:60
二、
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Student1(object):
@property #????
def birth(self):
return self._birth
@birth.setter #????
def birth(self, value):
self._birth = value
@property #????
def age(self):
return 2014 - self._birth
s = Student1()
s.birth = 60
print s.age
运行结果:60
相关文章推荐
- python Class:面向对象高级编程 @property
- Python 面向对象高级编程――使用@property
- python Class:面向对象高级编程 __str__ / __repr__
- python Class:面向对象高级编程 __call__& callable()
- python Class:面向对象高级编程 Enum(枚举)、@unique
- python Class:面向对象高级编程
- 【Python】学习笔记——-8.2、面向对象高级编程:2.使用@property
- python Class:面向对象高级编程 元类:type
- python Class: 面向对象高级编程 __iter__ 和 next()
- python Class:面向对象高级编程 元类:metaclass
- 初学Python的学习笔记9----面向对象编程(获取对象信息、实例属性和类属性)、面向对象高级编程(使用__slots__、使用@property)
- python Class:面向对象高级编程 __getitem__
- Python面向对象高级编程:@property--把方法变为属性
- python——(面向对象高级编程)使用@property
- python Class:面向对象高级编程 __getattr__
- 【Python】[面向对象高级编程] 使用__slots__,使用@property
- Python 第五章 面向对象高级编程
- python2.7学习笔记(10) ——面向对象高级编程
- python 面向对象高级编程
- Python学习笔记 - 面向对象高级编程