面向對象【day08】:靜態方法、類方法、屬性方法
本節內容
- 概述
- 靜態方法
- 類方法
- 屬性方法
- 總結
一、概述
前面我們已經講解了關於類的很多東西,今天講講類的另外的特性:靜態方法(staticmethod)、類方法(classmethod)、屬性方法(property)
二、靜態方法
2.1 定義
說明:[email protected],表示此方法為靜態方法
1 2 3 4 5 6 7 8 |
class Dog( object ):
def __init__( self ,name):
self .name = name
@staticmethod #在方法前加上staticmethod 裝飾器定義靜態方法 def eat():
print ( "dog is eating" )
|
2.2 靜態方法特性
特性:只是名義上歸類管理,實際上在靜態方法裏訪問不了類或實例中的任何屬性
①靜態方法,是不可以傳入self參數的,但是想傳也可以,調用時必須傳入實例本身
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Dog( object ):
def __init__( self ,name):
self .name = name
@staticmethod #定義靜態方法
def eat( self ,food): #可以定義,但是需傳入實例本身
print ( "{0} is eating {1}" . format ( self .name,food))
d = Dog( "shabi" )
d.eat(d, "hotdog" ) #傳入實例d本身,否則會報錯
#輸出
shabi is eating hotdog
|
②靜態方法可以用類直接調用,直接調用時,不可以直接傳入self,否則會報錯
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Dog( object ):
def __init__( self ,name): self .name = name
@staticmethod
def eat(food):
print ( "is eating {0}" . format (food))
Dog.eat( "hotdog" )
#輸出
is eating hotdog
|
2.3 場景
一般情況下我們需要使用工具包的一些個類的封裝,可以用靜態方法,比如os模塊
1 2 3 4 |
import os
os.system()
os.mkdir()
|
上面兩個方法沒有什麽必然的聯系在裏面,所以可以這麽用
三、類方法
3.1 定義
說明:[email protected],表示此方法為類方法
1 2 3 4 5 6 7 8 9 |
class Dog( object ):
name = "honggege" #定義靜態屬性
def __init__( self ,name):
self .name = name
@classmethod #定義類方法
def eat( self ,food):
print ( "{0} is eating {1}" . format ( self .name,food))
|
3.2 類方法特性
特性:只能訪問類變量(又叫靜態屬性),不能訪問實例變量
①訪問實例變量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Dog( object ):
def __init__( self ,name):
self .name = name
@classmethod #定義類方法
def eat( self ,food):
print ( "{0} is eating {1}" . format ( self .name,food))
d = Dog( "shabihong" )
d.eat( "hotdog" )
#輸出
File "D:/PycharmProjects/pyhomework/day7/類方法.py" , line 11 , in <module>
d.eat( "hotdog" )
File "D:/PycharmProjects/pyhomework/day7/類方法.py" , line 8 , in eat
print ( "{0} is eating {1}" . format ( self .name,food))
AttributeError: type object ‘Dog‘ has no attribute ‘name‘
|
②訪問類變量(又叫靜態屬性)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Dog( object ):
name = "honggege" #定義類變量
def __init__( self ,name):
self .name = name
@classmethod
def eat( self ,food):
print ( "{0} is eating {1}" . format ( self .name,food))
d = Dog( "shabihong" )
d.eat( "hotdog" )
#輸出
honggege is eating hotdog #調用的是類變量
|
3.3 使用場景
一個國家,有的國家不允許更改國籍,比如朝鮮,只能去訪問寫死的變量
四、屬性方法
4.1 定義
說明: [email protected],表示此方法為屬性方法
1 2 3 4 5 6 7 8 |
class Dog( object ):
def __init__( self ,name):
self .name = name
@property #定義屬性方法
def eat( self ):
print ( "{0} is eating" . format ( self .name))
|
4.2 特性
特性:把一個方法變成一個靜態屬性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Dog( object ):
def __init__( self ,name):
self .name = name
@property #定義屬性方法
def eat( self ):
print ( "{0} is eating" . format ( self .name))
d = Dog( "shabihong" )
d.eat #把方法變成靜態屬性調用
#輸出
shabihong is eating
|
我去,按照上面的用法,那我想傳入參數咋辦呢?
①給轉成的靜態屬性賦值
說明:[email protected],來給轉換後的靜態屬性賦值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Dog( object ):
def __init__( self ,name):
self .name = name
@property #定義屬性方法
def eat( self ):
print ( "{0} is eating {1}" . format ( self .name, "honggege" ))
@eat .setter #定義一個可以傳參的方法
def eat( self ,food):
print ( "set to food:" ,food)
# self.__food = food
d = Dog( "shabihong" )
d.eat = "hotdog" #給轉成的靜態變量賦值
d.eat
#輸出
set to food: hotdog
shabihong is eating honggege
|
那有些用同學說了,你上面還是沒有把food傳上去啊,好的,我們現在就來更深入的,請看如下代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Dog( object ):
def __init__( self ,name):
self .name = name
self .__food = None
@property #定義屬性方法
def eat( self ):
print ( "{0} is eating {1}" . format ( self .name, self .__food))
@eat .setter #定義可以設置變量賦值
def eat( self ,food):
print ( "set to food:" ,food)
self .__food = food
d = Dog( "shabihong" )
d.eat #第一份賦值的是None
d.eat = "hotdog"
d.eat #第二個賦值是hotdog
#輸出
shabihong is eating None
set to food: hotdog
shabihong is eating hotdog #說明賦值成功
|
②刪除轉變的靜態屬性
說明:[email protected],表明可以刪除轉化後的靜態屬性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Dog( object ):
def __init__( self ,name):
self .name = name
self .__food = None
@property
def eat( self ):
print ( "{0} is eating {1}" . format ( self .name, self .__food))
@eat .setter
def eat( self ,food):
print ( "set to food:" ,food)
self .__food = food
@eat .deleter #定義可以刪除eat這個靜態屬性
def eat( self ):
del self .__food
print ( "food 變量刪除完畢" )
d = Dog( "shabihong" )
del d.eat #刪除靜態屬性eat
#輸出
food 變量刪除完畢
|
4.3 使用場景
你想知道一個航班當前的狀態,是到達了、延遲了、取消了、還是已經飛走了, 想知道這種狀態你必須經歷以下幾步:
1. 連接航空公司API查詢
2. 對查詢結果進行解析
3. 返回結果給你的用戶
因此這個status屬性的值是一系列動作後才得到的結果,所以你每次調用時,其實它都要經過一系列的動作才返回你結果,但這些動作過程不需要用戶關心, 用戶只需要調用這個屬性就可以,明白 了麽?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class Flight( object ):
def __init__( self ,name):
self .flight_name = name
def checking_status( self ):
print ( "checking flight %s status " % self .flight_name)
return 1
@property
def flight_status( self ):
status = self .checking_status()
if status = = 0 :
print ( "flight got canceled..." )
elif status = = 1 :
print ( "flight is arrived..." )
elif status = = 2 :
print ( "flight has departured already..." )
else :
print ( "cannot confirm the flight status...,please check later" )
@flight_status .setter #修改
def flight_status( self ,status):
status_dic = {
0 : "canceled" ,
1 : "arrived" ,
2 : "departured"
}
print ( "\033[31;1mHas changed the flight status to \033[0m" ,status_dic.get(status) )
@flight_status .deleter #刪除
def flight_status( self ):
print ( "status got removed..." )
f = Flight( "CA980" )
f.flight_status
f.flight_status = 2 [email protected]_status.setter
del f.flight_status [email protected]_status.deleter
|
五、總結
- 靜態方法是訪問不了類或實例中的任何屬性,它已經脫離了類,一般會用在一些工具包中
- 類方法,只能訪問類變量,不能訪問實例變量
- 屬性方法是把一個方法變成一個靜態屬性
面向對象【day08】:靜態方法、類方法、屬性方法