Skip to content

Commit 0f78deb

Browse files
committed
@Property方法属性装饰器 😄
1 parent e862016 commit 0f78deb

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

property.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
__author__ = 'tanteng'
2-
3-
class Ver(object):
4-
def __init__(self,version):
5-
self.version = version
1+
# -*- coding: utf-8 -*-
62

3+
# 属性装饰器
4+
class Student(object):
75
@property
8-
def version_no(self):
9-
return self.version
6+
def score(self):
7+
return self._score
108

9+
@score.setter
10+
def score(self, value):
11+
if not isinstance(value,int):
12+
raise ValueError('必须输入数字!')
13+
if value<0 or value>100:
14+
raise ValueError('必须大于0小于100!')
15+
self._score = value
1116

12-
ver = Ver('2.0')
13-
print(ver.version_no)
17+
s = Student()
18+
s.score = 101
19+
print(s.score)

0 commit comments

Comments
 (0)