# Python 类
● class 类名(object):
● 简单类的创建
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__() 的特殊方法(构造方法)初始化类
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()
● 类的继承
class 子类(父类):
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()
● 类的继承
class 子类(父类1,父类2,父类3):
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()
● 子类函数(方法的重写)
父类方法不能满足需求,可以在子类重写父类的方法。
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