python編程普通及類和靜態(tài)方法示例詳解
前言
本文主要講述了python類中的三類常用方法,普通方法、類方法和靜態(tài)方法。
本文主要參考了https://youtu.be/rq8cL2XMM5M,強烈推薦一看這個系列的所有視頻。
運行環(huán)境
import sys sys.version
結(jié)果為
'3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]'
普通方法
我們這里定義一個叫做學(xué)生的類,并在其中定義了一個普通方法total_score()用于獲取某個實例的學(xué)生的總分。
class Student(object): num_of_stu = 0 #學(xué)生人數(shù) def __init__(self, name, age, math, Chinese): self.name = name #學(xué)生姓名 self.age = age #學(xué)生年齡 self.math = math #數(shù)學(xué)成績 self.Chinese = Chinese #語文成績 Student.num_of_stu += 1 #每實例化一個學(xué)生,人數(shù)加1,相當(dāng)于靜態(tài)變量 def __del__(self): Student.num_of_stu -= 1 #每刪除一個實例,人數(shù)減1 #普通方法,用于計算學(xué)生的總分 def total_score(self): return self.math + self.Chinese
然后我們生成幾個實例試一下看
print (Student.num_of_stu) stu1 = Student('Bob', 11, 51, 33) print (stu1.total_score()) stu2 = Student('Peco', 12, 98, 79) print (stu2.total_score()) print (Student.num_of_stu) del stu1 print (Student.num_of_stu)
結(jié)果為
0
84
177
2
1
類方法
現(xiàn)在假設(shè)我們想用一個字符串來實現(xiàn)實現(xiàn)一個實例的實例化,那么我們可以加上一個類方法from_string
class Student(object): num_of_stu = 0 #學(xué)生人數(shù) def __init__(self, name, age, math, Chinese): self.name = name #學(xué)生姓名 self.age = age #學(xué)生年齡 self.math = math #數(shù)學(xué)成績 self.Chinese = Chinese #語文成績 Student.num_of_stu += 1 #每實例化一個學(xué)生,人數(shù)加1,相當(dāng)于靜態(tài)變量 def __del__(self): Student.num_of_stu -= 1 #每刪除一個實例,人數(shù)減1 #普通方法,用于計算學(xué)生的總分 def total_score(self): return self.math + self.Chinese #類方法,用于用字符串生成實例 @classmethod def from_string(cls, stu_str): name, age, math, Chinese = stu_str.split('-') return cls(name, int(age), float(math), float(Chinese))
我們來試一下看
stu_str = "Bob-12-50-34" stu1 = Student.from_string(stu_str) print (stu1.name, stu1.total_score())
結(jié)果是
Bob 84.0
靜態(tài)方法
現(xiàn)在又假設(shè)我們需要類中有一個方法可以幫我們看看是不是上課日,那么我們就需要靜態(tài)方法了
class Student(object): num_of_stu = 0 #學(xué)生人數(shù) def __init__(self, name, age, math, Chinese): self.name = name #學(xué)生姓名 self.age = age #學(xué)生年齡 self.math = math #數(shù)學(xué)成績 self.Chinese = Chinese #語文成績 Student.num_of_stu += 1 #每實例化一個學(xué)生,人數(shù)加1,相當(dāng)于靜態(tài)變量 def __del__(self): Student.num_of_stu -= 1 #每刪除一個實例,人數(shù)減1 #普通方法,用于計算學(xué)生的總分 def total_score(self): return self.math + self.Chinese #類方法,用于用字符串生成實例 @classmethod def from_string(cls, stu_str): name, age, math, Chinese = stu_str.split('-') return cls(name, int(age), float(math), float(Chinese)) #靜態(tài)方法,用于判斷要不要上學(xué) @staticmethod def is_school_day(day): if day.weekday() == 5 or day.weekday() == 6: return False return True
我們來試下看
import datetime my_date1 = datetime.date(2017, 9, 15) my_date2 = datetime.date(2017, 9, 16) print (Student.is_school_day(my_date1)) print (Student.is_school_day(my_date2))
結(jié)果是
True
False
以上就是python編程普通及類和靜態(tài)方法示例詳解的詳細(xì)內(nèi)容,更多關(guān)于python方法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
修復(fù)CentOS7升級Python到3.6版本后yum不能正確使用的解決方法
這篇文章主要介紹了修復(fù)CentOS7升級Python到3.6版本后yum不能使用的問題,本文給大家?guī)砹私鉀Q方法,需要的朋友可以參考下2018-01-01Python+Tkinter實現(xiàn)經(jīng)典井字棋小游戲
Tkinter是內(nèi)置到Python安裝包中的,只要安裝好Python之后就能import?Tkinter,而且IDLE也是用Tkinter編寫而成的。本文將用Tkinter編寫經(jīng)典的井字棋小游戲,需要的可以參考一下2022-03-03