@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中文網其他相關文章!