在上篇“python中的類的創建、使用和繼承”中,創建了Person()和Student()兩個類,最后才是程式執行主體,如下:
class Person(): #創建一個person類,父類必須包含在當前檔案中,且位于子類前面, def __init__(self, name, age,hometown): #父類 self.name = name self.age = age self.hometown = hometown #屬性hometown def get_name(self): #定義列印名字的一個方法(函式) """ print("The person'name is "+self.name.title()+'.') def get_age(self): #定義列印年齡的一個方法(函式) """ print("The person is "+str(self.age)+' years old.') def get_grade(self): #定義列印家鄉的一個方法 """ print("The person is grade "+str(self.grade)) def play_game(self): #定義人游戲 """ print(self.name.title()+" is playing game with his friend !") def get_hometown(self): #定義列印學生家鄉的一個方法 """ print("The person "+self.name.title()+" is froming "+self.hometown.title()) class Student(Person): #定義子類Student,必須在括號內指定父類的名稱 def __init__(self, name, age,hometown): #方法 __init__()接受創建Person實體所需的資訊 super().__init__(name, age,hometown) self.grade = 7 #設定為默認值 7 def do_homework(self): #定義學生的一個方法(函式),做家庭作業""" print(self.name.title()+" is doing his homework now !") def get_student_info(self): #定義學生的一個方法(函式),列印學生資訊""" student_info = self.name.title()+' is '+str(self.age)+' years old, and from '+self.hometown +' and grade '+str(self.grade) print(student_info) def update_info(self,new_age,new_grade,new_hometown): #定義更新學生資訊一個方法(函式)""" self.age = new_age self.grade = new_grade self.hometown = new_hometown new_student=Student("jack",13,'NewYork') new_student.get_student_info() #列印學生全部資訊 print("\n") student_c=Student("Lily",14,'NewYork') student_c.get_student_info() #由于沒有修改grade,列印出來為默認值 7 print("\n") #修改實體student_c中的grade student_c.grade=9 #修改自己的的屬性grade為9 student_c.get_student_info() #列印學生全部資訊
隨著不斷地給類添加功能,檔案變得很長,為結構條理清晰,檔案整潔,可讀性增加,Python允許將類存盤在模塊中,然后在主程式中匯入所需的模塊,如同匯入模塊,
1)將上述Person()類和Student()類分別存盤為person和student兩個不同的檔案(模塊),同時將主要運行放置在一個主程式mainframe中,如下圖所示
其中person.py檔案中內容為:
class Person(): #創建一個person類,父類必須包含在當前檔案中,且位于子類前面, def __init__(self, name, age,hometown): #父類 self.name = name self.age = age self.hometown = hometown #屬性hometown def get_name(self): #定義列印名字的一個方法(函式) """ print("The person'name is "+self.name.title()+'.') def get_age(self): #定義列印年齡的一個方法(函式) """ print("The person is "+str(self.age)+' years old.') def get_grade(self): #定義列印家鄉的一個方法 """ print("The person is grade "+str(self.grade)) def play_game(self): #定義人游戲 """ print(self.name.title()+" is playing game with his friend !") def get_hometown(self): #定義列印學生家鄉的一個方法 """ print("The person "+self.name.title()+" is froming "+self.hometown.title())
student.py檔案中的內容為:
from person import * class Student(Person): #定義子類Student,必須在括號內指定父類的名稱 def __init__(self, name, age,hometown): #方法 __init__()接受創建Person實體所需的資訊 super().__init__(name, age,hometown) self.grade = 7 #設定為默認值 7 def do_homework(self): #定義學生的一個方法(函式),做家庭作業""" print(self.name.title()+" is doing his homework now !") def get_student_info(self): #定義學生的一個方法(函式),列印學生資訊""" student_info = self.name.title()+' is '+str(self.age)+' years old, and from '+self.hometown +' and grade '+str(self.grade) print(student_info) def update_info(self,new_age,new_grade,new_hometown): #定義更新學生資訊一個方法(函式)""" self.age = new_age self.grade = new_grade self.hometown = new_hometown
mainframe.py檔案中內容:
from student import * new_student=Student("jack",13,'NewYork') new_student.get_student_info() #列印學生全部資訊 print("\n") student_c=Student("Lily",14,'NewYork') student_c.get_student_info() #由于沒有修改grade,列印出來為默認值 7 print("\n") #修改實體student_c中的grade student_c.grade=9 #修改自己的的屬性grade為9 student_c.get_student_info() #列印學生全部資訊
student.py檔案,mainframe.py檔案中第一行 的from*** import * 陳述句 ,分別為打開模塊person和student,并匯入其中的Person類和Student類,同模塊功能一樣,
通過上述分塊,將類移到一個模塊中,并匯入該模塊,依然可以使用其所有功能,但主程式檔案變得整潔而易于閱讀,
二、多個類可以存放在一個模塊檔案中,例如,將上述的Person()類和Student()類放在同個檔案中,
class Person(): #創建一個person類,父類必須包含在當前檔案中,且位于子類前面, def __init__(self, name, age,hometown): #父類 self.name = name self.age = age self.hometown = hometown #屬性hometown def get_name(self): #定義列印名字的一個方法(函式) """ print("The person'name is "+self.name.title()+'.') def get_age(self): #定義列印年齡的一個方法(函式) """ print("The person is "+str(self.age)+' years old.') def get_grade(self): #定義列印家鄉的一個方法 """ print("The person is grade "+str(self.grade)) def play_game(self): #定義人游戲 """ print(self.name.title()+" is playing game with his friend !") def get_hometown(self): #定義列印學生家鄉的一個方法 """ print("The person "+self.name.title()+" is froming "+self.hometown.title()) class Student(Person): #定義子類Student,必須在括號內指定父類的名稱 def __init__(self, name, age,hometown): #方法 __init__()接受創建Person實體所需的資訊 super().__init__(name, age,hometown) self.grade = 7 #設定為默認值 7 def do_homework(self): #定義學生的一個方法(函式),做家庭作業""" print(self.name.title()+" is doing his homework now !") def get_student_info(self): #定義學生的一個方法(函式),列印學生資訊""" student_info = self.name.title()+' is '+str(self.age)+' years old, and from '+self.hometown +' and grade '+str(self.grade) print(student_info) def update_info(self,new_age,new_grade,new_hometown): #定義更新學生資訊一個方法(函式)""" self.age = new_age self.grade = new_grade self.hometown = new_hometown
匯入同一檔案模塊中的多個類,可直接使用,從一個模塊中匯入多個類時,用逗號分隔了各個類,匯入必要的類后,可根據需求,創建每個類的任意數量的實體,
from person import Person,Student

匯入整個模塊,再使用“模塊名.類名” 表示法訪問需要的類,如同使用實體的方法一樣,如下圖所示:

Python標準庫是一組模塊,安裝的 Python 都包含它,可使用標準庫中的任何函式和類,為此只需在程式開頭包含一條簡單的 import 陳述句
類、模塊的命名規則:
1)類名應采用駝峰命名法,即將類名中的每個單詞的首字母都大寫,而不使用下劃線,
實體名和模塊名都采用小寫格式,并在單詞之間加上下劃線,
2)定義類,后面包含一個簡要地描述類的功能檔案字串,并遵循撰寫函式的檔案字串時采用的格式約定,
模塊也都應包含一個檔案字串,對其中的類可用于做什么進行描述,
3)可使用空行來組織代碼,但不要濫用,在類中,可使用一個空行來分隔方法;在模塊中,可使用兩個空行來分隔類,
4)同時匯入標準庫中的模塊和自己撰寫的模塊時,先匯入標準庫模塊的 import 陳述句,再添加一個空行,再匯入自己撰寫的模塊的 import 陳述句,讓人更容易明白程式使用的各個模塊都來自何方
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/459509.html
標籤:Python
上一篇:python模塊
