python classmethod,staticmethod,property 一網打盡
阿新 • • 發佈:2019-01-03
廢話不多說 看粗糙的程式碼
1 # -*- coding:utf-8 -*- 2 3 class A(object): 4 nn="qwert" 5 def __init__(self): 6 self.nn="xoxo" 7 8 def bar(self): 9 print(self.nn) 10 @classmethod 11 def func(cls): 12 return cls() 13 14 @staticmethod 15 deffoo(name): 16 print("hello"+name+A.nn) 17 18 @property 19 def score(self): 20 return self._score 21 22 @score.setter 23 def score(self,value): 24 if not isinstance(value,int): 25 raise ValueError("score must be an integer") 26 27if value<0 or value>100: 28 raise ValueError("score must between 0-100!") 29 30 self._score=value 31 32 33 34 print(A.nn) 35 A().bar() 36 print(A().bar) 37 print(A.func().nn) 38 A.foo("fuck") 39 s=A() 40 s.score=60 41 print(s.score)
列印結果:qwert
xoxo
<bound method A.bar of <__main__.A object at 0x00000239BB88F7B8>>
xoxo
hellofuckqwert
60
======敲一遍打印出來基本就瞭解了======