记录#
今天我学习了面向对象编程(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()