MulaiMulai sekarang secara gratis

Create a subclass

The purpose of child classes, or sub-classes, is to customize and extend the functionality of the parent class.

Recall the Employee class from earlier in the course. In most organizations, managers have more privileges and responsibilities than regular employees. So it would make sense to introduce a Manager class that has more functionality than Employee.

But a Manager is still an employee, so the Manager class should be inherited from the Employee class.

In this exercise, you'll create a Manager child class and, later in the course, you'll add specific functionality to the class.

Latihan ini adalah bagian dari kursus

Introduction to Object-Oriented Programming in Python

Lihat Kursus

Petunjuk latihan

  • Add a Manager class that inherits from Employee.
  • Use a keyword to leave the Manager class empty.
  • Create an object called mng using the Manager class, setting the name to "Debbie Lashko" and salary to 86500.
  • Print the name attribute of mng.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

class Employee:
    MIN_SALARY = 30000    

    def __init__(self, name, salary=MIN_SALARY):
        self.name = name
        if salary >= Employee.MIN_SALARY:
            self.salary = salary
        else:
            self.salary = Employee.MIN_SALARY
        
    def give_raise(self, amount):
        self.salary += amount      
        
# Define a new class Manager inheriting from Employee
____
    # Add a keyword to leave this class empty
    ____

# Define a Manager object
mng = ____

# Print mng's name
print(____)
Edit dan Jalankan Kode