天天看點

學習Python③:Python中的類、對象(繼承和多态)、子產品、包

今天學習了Python的類和對象,包括繼承和多态,還有子產品和包的使用,通過代碼學習,記錄下來以備後查。

​# 類和object   所有的資料類型都是對象,

import datetime                                      # 内置子產品

from sys import path,version_info                    # 内置子產品,隻引用sys子產品中的path和version_info,如果是*代表引用所有的

if 'G:\\Python\\demo\\demo' not in path:             # 確定目前開發路徑名在sys.path中

   path.append('G:\\Python\\demo\\demo')

print(version_info)

from BaseFunction.CommFunction import Sum as MySum   # 自己寫的子產品Sum,在CommFunciton.py檔案中,新的使用名稱為MySum

class Student:

   count=0

   @classmethod                                     # 定義類的方法

   def StatisticalMembers(cls):                     # cls代表類本身

       print("目前有{}個成員。".format(cls.count))

   @staticmethod                                    # 靜态方法

   def SayHello(name):

       print("{},Student類有{}個執行個體.".format(name,Student.count))

   def __init__(self,Name,Age,Hobby,Grade):         # 構造函數:self代表類本身

       Student.count=Student.count+1                # 統計有多少個執行個體化對象

       self.__Name=Name

       self.__Age=Age

       self.__Hobby=Hobby

       self.__Grade=Grade                           # __Grade是私有屬性(變量名前面加__),隻有在類的内部可以通路和修改。

   def SelfIntroduction(self):                # 第一個參數必須是self

       print("Hello, my name is {}, I'm {} years old,my hobby is {}, I'm in the {} grade.".format(self.__Name,self.__Age,self.__Hobby,self.__Grade))

   @property                                        # 定義屬性

   def Name(self):                                  # 擷取__Name屬性

       return self.__Name.upper()

   @Name.setter                                     # 設定__Name屬性

   def Name(self,Name):

   def Age(self):

       return self.__Age

   def Hobby(self):

       return self.__Hobby

   def Grade(self):

       return self.__Grade

HaoR=Student(Name='HaoR',Age=12,Hobby='mathematics',Grade=7)

HaoR._Student__Grade=12                               # 通過内部的參數通路受保護的參數__Grade,前面加_Student

# HaoR.SelfIntroduction()

Student.StatisticalMembers()

# 繼承和多态

class NewStudent(Student):

   def SelfIntroduction(self):

       print("Hello, my name is {}, today is {}.".format(self.Name,datetime.date.today()))

XinR = NewStudent('XinR', 19, 'singing', 19)

NewStudent.StatisticalMembers()

XinR.Name = 'WeiW'

# XinR.SelfIntroduction()

def SelfIntroduction(HaoR):

   HaoR.SelfIntroduction()

for item in [HaoR,XinR]:

   SelfIntroduction(item)

   print(isinstance(XinR, Student), '\n', isinstance(HaoR, NewStudent))        # 判斷是否是繼承

Student.SayHello("Python 3.7")

# 子產品module和包

print(MySum(19,3,5,7,39))

'''

傳回的結果:

sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)

目前有1個成員。

目前有2個成員。

Hello, my name is HaoR, I'm 12 years old,my hobby is mathematics, I'm in the 12 grade.

True  

False

Hello, my name is WEIW, today is 2021-01-10.

Python 3.7,Student類有2個執行個體.

73

​CommFunction.py裡面的函數:

​def Sum(*args):

   Result=0

   for item in args:

       Result=Result+item

   return Result

if __name__=='__main__':               # 别人引用的時候就不出現下面的代碼

   print('測試:',Sum(1,2,3))