@staticmethod 装饰后,类中的方法转成静态方法
<span style="color: #008080">1</span> <span style="color: #0000ff">class</span><span style="color: #000000"> a: </span><span style="color: #008080">2</span> <span style="color: #008080">3</span> <span style="color: #000000"> @staticmethod </span><span style="color: #008080">4</span> <span style="color: #0000ff">def</span><span style="color: #000000"> b(self): </span><span style="color: #008080">5</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">''</span>)
静态方法不可以访问实例变量或类变量,相当于类中的工具包。如os, system 等 import的模块一般
@classmethod装饰后,类中的方法转成类方法,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量
<span style="color: #008080">1</span> <span style="color: #0000ff">class</span><span style="color: #000000"> b(object): </span><span style="color: #008080">2</span> name=<span style="color: #800000">'</span><span style="color: #800000">aa</span><span style="color: #800000">'</span> <span style="color: #008080">3</span> <span style="color: #000000"> @classmethod </span><span style="color: #008080">4</span> <span style="color: #0000ff">def</span><span style="color: #000000"> cc(self): </span><span style="color: #008080">5</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">%s .l.....</span><span style="color: #800000">'</span>%<span style="color: #000000">name) </span><span style="color: #008080">6</span> <span style="color: #008080">7</span> <span style="color: #008080">8</span> <span style="color: #008080">9</span> b.cc()
@property装饰后把,类中的方法方法转成静态属性
静态属性的使用方法
<span style="color: #008080"> 1</span> <span style="color: #0000ff">class</span><span style="color: #000000"> Eat_food(object): </span><span style="color: #008080"> 2</span> self.<span style="color: #800080">__food</span>=None<span style="color: #008000">#</span><span style="color: #008000">设置一个私有属性</span> <span style="color: #008080"> 3</span> <span style="color: #008080"> 4</span> <span style="color: #000000"> @property </span><span style="color: #008080"> 5</span> <span style="color: #0000ff">def</span><span style="color: #000000"> eat(self): </span><span style="color: #008080"> 6</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">.....%s</span><span style="color: #800000">'</span>%self.<span style="color: #800080">__food</span><span style="color: #000000">) </span><span style="color: #008080"> 7</span> <span style="color: #008080"> 8</span> @eat.setter <span style="color: #008000">#</span><span style="color: #008000">再次装饰后可以对其赋值</span> <span style="color: #008080"> 9</span> <span style="color: #0000ff">def</span><span style="color: #000000"> eat(self,food): </span><span style="color: #008080">10</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">.....%s</span><span style="color: #800000">'</span>%<span style="color: #000000">food) </span><span style="color: #008080">11</span> self.<span style="color: #800080">__food</span>=food<span style="color: #008000">#</span><span style="color: #008000">保存到私有属性,备用</span> <span style="color: #008080">12</span> <span style="color: #008080">13</span> <span style="color: #000000"> @eat.deleter </span><span style="color: #008080">14</span> <span style="color: #0000ff">def</span><span style="color: #000000"> eat(self): </span><span style="color: #008080">15</span> <span style="color: #0000ff">del</span> self.<span style="color: #800080">__food</span><span style="color: #008000">#</span><span style="color: #008000">删除保存赋值的私有属性,相当于删除这个属性方法</span>
以上是python第三十三天----静态方法、类方法、属性方法的详细内容。更多信息请关注PHP中文网其他相关文章!