記錄#
今天我學習了面向對象編程(OOP)。在面向對象編程中,首先需要創建一個類,使用關鍵字class
後面跟著類名。當我們需要使用這個類的時候,我們稱之為實例化對象。
對象本身是一個模板,沒有具體的運行意義。只有在實例化之後,對象才具有具體的價值。子類可以繼承父類的信息,並且在此基礎上進行擴展。
作為今天的作業,我創建了一個名為Job
的類,供Lawyer
調用。同時,我還創建了Teacher
和Doctor
子類,它們繼承了Job
類的信息,並在此基礎上進行了擴展。
CODE#
class job:
name = None
salary = None
hours = None
def __init__(self, name, salary, hours):
self.name = name
self.salary = salary
self.hours = hours
def print(self):
print(f"{self.name} > {self.salary} > {self.hours}")
class doctor(job):
speciality = None
years = None
def __init__(self, name, salary, hours, speciality, years):
self.name = name
self.salary = salary
self.hours = hours
self.speciality = speciality
self.years = years
def print(self):
print(
f"{self.name} > {self.salary} > {self.hours} > {self.speciality} > {self.years}"
)
class teacher(job):
subject = None
position = None
def __init__(self, name, salary, hours, subject, position):
self.name = name
self.salary = salary
self.hours = hours
self.subject = subject
self.position = position
def print(self):
print(
f"{self.name} > {self.salary} > {self.hours} > {self.subject} > {self.position}"
)
laywer = job("張三", "1萬", "8小時")
doctor1 = doctor("李四", "2萬", "9小時", "兒科", "十年")
teacher1 = teacher("王五", "3萬", "10小時", "計算機", "教授")
laywer.print()
doctor1.print()
teacher1.print()