Skip to content

Commit ad00a75

Browse files
committed
property
1 parent 5b70bc1 commit ad00a75

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#Python允许使用多重继承 采用MixIn就是一种常见的设计
2+
3+
4+
class Animal(object):
5+
pass
6+
7+
8+
#大类
9+
class Manmal(Animal):
10+
pass
11+
12+
13+
class Bird(Animal):
14+
pass
15+
16+
17+
#功能
18+
class RunnableMixIn(object):
19+
pass
20+
21+
22+
class CarnivorousMixIn(object):
23+
pass
24+
25+
26+
#MixIn 设计方式
27+
class Dog(Manmal, RunnableMixIn, CarnivorousMixIn):
28+
pass
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#用装饰器函数把 get/set 方法“装饰”成属性调用:
2+
3+
4+
class Student(object):
5+
def __init__(self, name, score):
6+
self.name = name
7+
self.__score = score
8+
9+
@property
10+
def score(self):
11+
return self.__score
12+
13+
@score.setter #setter 是property的装饰后的副产品
14+
def score(self, score):
15+
if score < 0 or score > 100:
16+
raise ValueError('invaild score')
17+
self.__score = score
18+
19+
20+
s = Student('coa', 90)
21+
s.score = 80
22+
print(s.score)

0 commit comments

Comments
 (0)