標籤:ret ... inf str 名稱 無法 pytho 父類 each
class Person: """人員資訊""" # 姓名(共有屬性) name = ‘‘ # 年齡(共有屬性) age = 0 #定義建構函式 def __init__(self, name=‘‘, age=0): self.name = name self.age = age # 重載專有方法: __str__ def __str__(self): return "這裡重載了 __str__ 專有方法, " + str({‘name‘: self.name, ‘age‘: self.age}) def set_age(self, age): self.age = ageclass Account: """賬戶資訊""" # 賬戶餘額(私人屬性) __balance = 0 # 所有賬戶總額 __total_balance = 0 # 擷取賬戶餘額 # self 必須是方法的第一個參數 def balance(self): return self.__balance ‘‘‘一共有三種方式定義類的方法 第一種:不加修飾,綁定的是當前的執行個體 增加賬戶餘額‘‘‘ def balance_add(self, cost): # self 訪問的是本執行個體 self.__balance += cost # self.__class__ 可以訪問類 self.__class__.__total_balance += cost ‘‘‘ 第二種:用@classmethod來修飾,綁定的是類 類方法(用@classmethod標識,第一個參數為 cls) ‘‘‘ @classmethod def total_balance(cls): return cls.__total_balance ‘‘‘ 第二種:用@staticmethod修飾,不綁定 靜態方法(用 @staticmethod 標識,不需要類參數或執行個體參數) ‘‘‘ @staticmethod def exchange(a, b): return b, aclass Teacher(Person, Account): """教師""" # 班級名稱,受保護的屬性 _class_name = ‘‘ def __init__(self, name): # 第一種重載父類__init__()構造方法 # super(子類,self).__init__(參數1,參數2,....) super(Teacher, self).__init__(name) @classmethod def set_class_name(cls,class_name): cls._class_name = class_name def get_info(self): # 以字典的形式返回個人資訊 return { ‘name‘: self.name, # 此處訪問的是父類Person的屬性值 ‘age‘: self.age, ‘class_name‘: self._class_name, ‘balance‘: self.balance(), # 此處調用的是子類重載過的方法 } # 方法重載 def balance(self): # Account.__balance 為私人屬性,子類無法訪問,所以父類提供方法進行訪問 return Account.balance(self) * 1.1class Student(Person, Account): """學生""" _teacher_name = ‘‘ def __init__(self, name, age=18): # 第二種重載父類__init__()構造方法 # 父類名稱.__init__(self,參數1,參數2,...) Person.__init__(self, name, age) @classmethod def set_teacher_name(cls,teacher_name): cls._teacher_name = teacher_name def get_info(self): # 以字典的形式返回個人資訊 return { ‘name‘: self.name, # 此處訪問的是父類Person的屬性值 ‘age‘: self.age, ‘teacher_name‘: self._teacher_name, ‘balance‘: self.balance(), }# 教師 Johnjohn = Teacher(‘John‘)john.set_class_name(‘CS2018‘)john.balance_add(20)john.set_age(36) # 子類的執行個體可以直接調用父類的方法print("John‘s info:", john.get_info())# 學生 Marymary = Student(‘Mary‘, 18)mary.balance_add(18)mary.set_teacher_name(‘john‘)print("Mary‘s info:", mary.get_info())# 學生 Fakefake = Student(‘Fake‘)fake.balance_add(30)print("Fake‘s info", fake.get_info())# 三種不同的方式調用靜態方法print("john.exchange(‘a‘, ‘b‘):", john.exchange(‘a‘, ‘b‘))print(‘Teacher.exchange(1, 2)‘, Teacher.exchange(1, 2))print(‘Account.exchange(10, 20):‘, Account.exchange(10, 20))# 類方法、類屬性print(‘Account.total_balance():‘, Account.total_balance())print(‘Teacher.total_balance():‘, Teacher.total_balance())print(‘Student.total_balance():‘, Student.total_balance())# 重載專有方法print(john)print(mary)print(fake)
輸出:
John‘s info: {‘name‘: ‘John‘, ‘age‘: 36, ‘balance‘: 22.0, ‘class_name‘: ‘CS2018‘}
Mary‘s info: {‘teacher_name‘: ‘john‘, ‘name‘: ‘Mary‘, ‘age‘: 18, ‘balance‘: 18}
Fake‘s info {‘teacher_name‘: ‘john‘, ‘name‘: ‘Fake‘, ‘age‘: 18, ‘balance‘: 30}
john.exchange(‘a‘, ‘b‘): (‘b‘, ‘a‘)
Teacher.exchange(1, 2) (2, 1)
Account.exchange(10, 20): (20, 10)
Account.total_balance(): 0
Teacher.total_balance(): 20
Student.total_balance(): 48
這裡重載了 __str__ 專有方法, {‘name‘: ‘John‘, ‘age‘: 36}
這裡重載了 __str__ 專有方法, {‘name‘: ‘Mary‘, ‘age‘: 18}
這裡重載了 __str__ 專有方法, {‘name‘: ‘Fake‘, ‘age‘: 18}
Python3面向i對象編程執行個體