python實(shí)例方法的使用注意
1、實(shí)例方法是從屬于實(shí)例對(duì)象的方法,定義實(shí)例方法時(shí),第一個(gè)參數(shù)必須為self。self指當(dāng)前的實(shí)例對(duì)象。
2、調(diào)用實(shí)例方法是,不需要也不能給self傳值,self由解釋器自動(dòng)傳參。
實(shí)例
classgetMin():
#實(shí)例方法
deffun(self,arr,n):
print(arr[n-1])
#類方法
@classmethod
defclass_fun(cls):
print("thisisclassfunction")
if__name__=="__main__":
arr=input().strip().split("")
int_arr=[]
foriteminarr:
int_arr.append(int(item))
n=int(input())
instance=getMin()
#用實(shí)例調(diào)用實(shí)例方法
instance.fun(int_arr,n)
#用類調(diào)用方法
getMin.fun(instance,int_arr,n)
#實(shí)例調(diào)用類方法
instance.class_fun()
#類調(diào)用類方法
getMin.class_fun()
以上就是python實(shí)例方法的使用注意,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)教程請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。