This example shows how a class extends and overrides methods. Employee is the base class. Manager inherits from it, adds a department field, and rewrites get_employee_info into its own version. Department uses class methods to manage the whole team of staff.
class Employee:
numberOfEmployee=0
def __init__(self, name, salary, city):
self.name = name
self.salary = salary
self.city = city
Employee.numberOfEmployee += 1
def getInfo(self):
return (f'name:{self.name}, salary:{self.salary}, city:{self.city}')
def getSalary(self):
return self.salary
def get_employee_info(self):
return f'{self.name}, Salary:{self.salary}, City:{self.city}'
def __del__ (self):
Employee.numberOfEmployee -=1
class Manager(Employee):
#extend class
def __init__(self, name, salary, city, department):
super(). __init__(name, salary, city)
self.department = department
#override
def get_employee_info(self):
return f'{self.name} (Manager of {self.department}, City: {self.city})'
class Department:
companyName = 'TTL'
def __init__(self, name):
self.name=name
self.employee=[]
self.budget=0
@classmethod
def getCompanyName(cls):
return cls.companyName
def setBudget(self, amount):
self.budget=amount
return
def getBudget(self):
return self.budget
def addEmployee (self, staff):
self.employee.append(staff)
def getEmployee(self):
return self.employee
def getTotalStaffCost(self):
TotalStaffCost=0
return sum([employee.salary for employee in self.employee])
# for i in self.employee:
# TotalStaffCost += i.salary
# return TotalStaffCost
def getSalaryAverage(self):
return self.getTotalStaffCost()/len(self.employee)
def get_employee_info(self):
return f'{self.name}, {len(self.employee)} employee(s)'
@classmethod
def getNumberOfEmployee(cls):
return cls.numberOfEmployee
@classmethod
def setCompanyName (cls, name):
cls.companyName = name
emp1 = Employee('Peter', 10000, 'KT')
emp2 = Employee('John', 15000, 'HK')
employees = []
employees.append(emp1)
employees.append(emp2)
# print(employees[0].getInfo())
employees.append(Employee('Mary', 50000, 'KT'))
totalSalary=0
for i in employees:
totalSalary += i.getSalary()
print (f'Total Salary= {totalSalary}')
print (f'Average Salary= {totalSalary/len(employees)}')
hr=Department('HR')
it=Department('IT')
hr.addEmployee(Employee('Peter', 10000, 'KT'))
hr.addEmployee(Employee('John', 15000, 'KT'))
hr.addEmployee(Manager('ManagerWong', 30000, 'KT', 'HR Department'))
for employee in hr.employee:
print(employee.get_employee_info())
output:
Total Salary= 75000 Average Salary= 25000.0 Peter, Salary:10000, City:KT John, Salary:15000, City:KT ManagerWong (Manager of HR Department, City: KT)
- Inheritance (extend): put the parent class in the parentheses — class Manager(Employee) — and Manager automatically gets all of Employee’s attributes and methods, no rewriting needed.
- Overriding: Manager defines a same-named get_employee_info with different content. Call it on a Manager object and the Manager version runs (see the last line of the output — the format is different).
- Class method (@classmethod): the first parameter is cls (the class itself), so you can use it without creating an object, e.g. getCompanyName.
- Class variable: numberOfEmployee belongs to the class level and is shared by all objects. __init__ increments it and __del__ decrements it, tracking the total headcount.
