Record#
Today I learned about Object-Oriented Programming (OOP). In OOP, the first step is to create a class using the keyword class
followed by the class name. When we need to use this class, we refer to it as instantiating an object.
An object itself is a template and does not have any specific meaning until it is instantiated. Subclasses can inherit information from parent classes and extend it further.
As today's assignment, I created a class called Job
for use by Lawyer
. Additionally, I created subclasses Teacher
and Doctor
which inherit information from the Job
class and extend it further.
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()