# Python 類
● class 類名(object):
json
● 簡單類的建立windows
1 # -*- coding: UTF-8 -*- 2 class FirstClass: 3 """建立一個簡單的類""" 4 nValue = 12345 5 def outString(self): 6 return 'hello world' 7 8 # 實例化類 9 x = FirstClass() 10 11 # 訪問類的屬性和方法 12 print("FirstClass 類的屬性 i 爲:", x.nValue) 13 print("FirstClass 類的方法 f 輸出爲:", x.outString())
● __init__() 的特殊方法(構造方法)初始化類app
1 # -*- coding: UTF-8 -*- 2 class FirstClass: 3 """建立一個簡單的類""" 4 def __init__(sum, x, y): 5 sum.x = x 6 sum.y = y 7 8 9 Go = FirstClass(9,「HelloWorld」) 10 print(Go.x, Go.y)
● def 定義類函數函數
1 # -*- coding: UTF-8 -*- 2 #類定義 3 class People: 4 #定義基本屬性 5 name = '' 6 age = 0 7 #定義私有屬性,私有屬性在類外部沒法直接進行訪問 8 __weight = 0 9 #定義構造方法 10 def __init__(self,name,age,w): 11 self.name = name 12 self.age = age 13 self.__weight = w 14 def speak(self): 15 print("%s 說: 我 %d 歲。" %(self.name,self.age)) 16 17 # 實例化類 18 p = People("☆__夜__☆",23,30) 19 p.speak()
● 類的繼承spa
class 子類(父類):code
1 #類定義 2 class people: 3 #定義基本屬性 4 name = '' 5 age = 0 6 #定義私有屬性,私有屬性在類外部沒法直接進行訪問 7 __weight = 0 8 #定義構造方法 9 def __init__(self,n,a,w): 10 self.name = n 11 self.age = a 12 self.__weight = w 13 def speak(self): 14 print("%s 說: 我 %d 歲。" %(self.name,self.age)) 15 16 #單繼承示例 17 class student(people): 18 grade = '' 19 def __init__(self,n,a,w,g): 20 #調用父類的構函 21 people.__init__(self,n,a,w) 22 self.grade = g 23 #覆寫父類的方法 24 def speak(self): 25 print("%s 說: 我 %d 歲了,我在讀 %d 年級"%(self.name,self.age,self.grade)) 26 27 28 29 s = student('☆__夜__☆',23,60,99) 30 s.speak()
● 類的繼承orm
class 子類(父類1,父類2,父類3):blog
1 #類定義 2 #類定義 3 class people: 4 #定義基本屬性 5 name = '' 6 age = 0 7 #定義私有屬性,私有屬性在類外部沒法直接進行訪問 8 __weight = 0 9 #定義構造方法 10 def __init__(self,n,a,w): 11 self.name = n 12 self.age = a 13 self.__weight = w 14 def speak(self): 15 print("%s 說: 我 %d 歲。" %(self.name,self.age)) 16 17 #單繼承示例 18 class student(people): 19 grade = '' 20 def __init__(self,n,a,w,g): 21 #調用父類的構函 22 people.__init__(self,n,a,w) 23 self.grade = g 24 #覆寫父類的方法 25 def speak(self): 26 print("%s 說: 我 %d 歲了,我在讀 %d 年級"%(self.name,self.age,self.grade)) 27 28 #另外一個類,多重繼承以前的準備 29 class job(): 30 topic = '' 31 name = '' 32 def __init__(self,n,t): 33 self.name = n 34 self.topic = t 35 def speak(self): 36 print("我叫 %s,個人工做是%s"%(self.name,self.topic)) 37 38 #多重繼承 39 class sample(job,student): 40 a ='' 41 def __init__(self,n,a,w,g,t): 42 student.__init__(self,n,a,w,g) 43 job.__init__(self,n,t) 44 45 test = sample("☆__夜__☆",25,80,4,"IT 碼農 T_T") 46 test.speak()
● 子類函數(方法的重寫)
繼承
父類方法不能知足需求,能夠在子類重寫父類的方法。get
1 class Parent: # 定義父類 2 def MyFun(self): 3 print ('正在調用父類函數...........') 4 5 class Child(Parent): # 定義子類 6 def MyFun(self): 7 print ('正在調用子類函數...........') 8 9 Son = Child() # 子類實例 10 Son.MyFun() # 子類調用重寫方法
做業詳解
1 # -*- coding: UTF-8 -*- 2 #poedu_shop.py 3 import useroperator 4 def main(): 5 if useroperator.user_login(input("You Name:"),input("You Password:"),"userdata.txt"): 6 pass 7 else: 8 pass 9 pass 10 11 if __name__ == "__main__": 12 main() 13 else: 14 print("請從poedu_shop.py啓動程序")
1 # -*- coding: UTF-8 -*- 2 #poedu_file.py 3 4 import os.path 5 6 def get_file_suffix(path): 7 return os.path.split(path)[1] #獲取文件後綴名 8 9 def __get_file_format_type(file): 10 suffix = get_file_suffix(file) 11 if suffix == '.txt': 12 return 'pickle','wb' 13 elif suffix == '.json': 14 return 'json','w' 15 16 def dump_file(file,data): #新建用戶名,寫入文件 17 18 modulename = __get_file_format_type(file) 19 if modulename != None: 20 module = __import__(modulename[0]) 21 with open(file,modulename[1]) as f: 22 module.dump(data,f) 23 24 25 def load_file(file): 26 try: 27 modulename == __get_file_format_type(file) 28 if modulename != None: 29 module = __import__(modulename) 30 with open(file,'rb') as f: 31 return module.load(f) 32 else: 33 print("不支持的格式!") 34 except: 35 print("文件不存在!") 36 37 38 """ 39 def load_file(file): 40 #.json .txt 41 #userdata c://windows// 42 if get_file_suffix(file) == '.txt': 43 module = __import__('pickle') 44 elif get_file_suffix(file) == '.json': 45 module = __import__('josn') 46 else: 47 print("不支持的文件格式") 48 return 49 try: 50 with open(file,'rb') as f: 51 return module.load(f) 52 except: 53 print("文件不存在") 54 """
1 # -*- coding: UTF-8 -*- 2 #useroperator.py 3 import poedu_file 4 all_user_infos = [] 5 def user_new(name,psw,money,salary,file): #接收參數,新建文件 6 all_user_infos.append({'name':name,'password':psw,'salary':salary,'money':money}) 7 poedu_file.dump_file(file,all_user_infos) 8 9 def user_login(name, psw, file): 10 user_datas = poedu_file.load_file(file) 11 if user_datas == None: 12 #選擇新建 13 user_new(name,psw,1000,500,file) 14 else: 15 for user_info in user_datas: 16 if name == user_info['name'] and psw == user_info['password']: 17 pass 18 pass