Kế thừa đa cấp có được hỗ trợ trong python không?

Kế thừa đa cấp có được hỗ trợ trong python không?



  • Kế thừa đa cấp cũng có thể thực hiện được trong Python không giống như các ngôn ngữ lập trình khác
  • Bạn có thể kế thừa một lớp dẫn xuất từ ​​một lớp dẫn xuất khác
  • Điều này được gọi là kế thừa đa cấp. Trong Python, kế thừa đa cấp có thể được thực hiện ở bất kỳ độ sâu nào

  • Một ví dụ với trực quan hóa tương ứng được đưa ra dưới đây

class Base:
    pass

class Derived1(Base):
    pass

class Derived2(Derived1):
    pass

Kế thừa đa cấp có được hỗ trợ trong python không?

Thí dụ. 1 Kế thừa đa cấp Python

#!/usr/bin/evn python

# Define a class as 'student'
class student:
    # Method
    def getStudent(self):
        self.name = input("Name: ")
        self.age = input("Age: ")
        self.gender = input("Gender: ")

# Define a class as 'test' and inherit base class 'student'
class test(student):
    # Method
    def getMarks(self):
        self.stuClass = input("Class: ")
        print("Enter the marks of the respective subjects")
        self.literature = int(input("Literature: "))
        self.math = int(input("Math: "))
        self.biology = int(input("Biology: "))
        self.physics = int(input("Physics: "))

# Define a class as 'marks' and inherit derived class 'test'
class marks(test):
    # Method
    def display(self):
        print("\n\nName: ",self.name)
        print("Age: ",self.age)
        print("Gender: ",self.gender)
        print("Study in: ",self.stuClass)
        print("Total Marks: ", self.literature + self.math + self.biology + self.physics)


m1 = marks()
# Call base class method 'getStudent()'
m1.getStudent()
# Call first derived class method 'getMarks()'
m1.getMarks()
# Call second derived class method 'display()'
m1.display()

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211

Ví dụ2. Kế thừa đa cấp Python

class Animal:
def eat(self):
print 'Eating...'
class Dog(Animal):
def bark(self):
print 'Barking...'
class BabyDog(Dog):
def weep(self):
print 'Weeping...'
d=BabyDog()
d.eat()
d.bark()
d.weep()

Eating...
Barking...
Weeping


Trong bài viết này, bạn sẽ khám phá tính kế thừa và thành phần trong Python. Kế thừa và thành phần là hai khái niệm quan trọng trong lập trình hướng đối tượng, mô hình hóa mối quan hệ giữa hai lớp. Chúng là các khối xây dựng của thiết kế hướng đối tượng và chúng giúp các lập trình viên viết mã có thể tái sử dụng

Đến cuối bài viết này, bạn sẽ biết cách

  • Sử dụng thừa kế trong Python
  • Mô hình phân cấp lớp bằng cách sử dụng tính kế thừa
  • Sử dụng đa kế thừa trong Python và hiểu nhược điểm của nó
  • Sử dụng bố cục để tạo các đối tượng phức tạp
  • Sử dụng lại mã hiện có bằng cách áp dụng thành phần
  • Thay đổi hành vi của ứng dụng trong thời gian chạy thông qua thành phần

Tiền thưởng miễn phí. Nhấp vào đây để có quyền truy cập vào Bảng cheat Python OOP miễn phí chỉ cho bạn các hướng dẫn, video và sách hay nhất để tìm hiểu thêm về Lập trình hướng đối tượng với Python

Kế thừa và Thành phần là gì?

Kế thừa và thành phần là hai khái niệm chính trong lập trình hướng đối tượng, mô hình hóa mối quan hệ giữa hai lớp. Họ thúc đẩy thiết kế của một ứng dụng và xác định cách ứng dụng sẽ phát triển khi các tính năng mới được thêm vào hoặc các yêu cầu thay đổi

Cả hai đều cho phép tái sử dụng mã, nhưng chúng thực hiện theo những cách khác nhau

Loại bỏ các quảng cáo

Kế thừa là gì?

Các mô hình kế thừa cái được gọi là một mối quan hệ. Điều này có nghĩa là khi bạn có một lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
6 kế thừa từ một lớp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
7, bạn đã tạo một mối quan hệ trong đó
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
6 là một phiên bản chuyên biệt của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
7

Tính kế thừa được biểu diễn bằng Ngôn ngữ mô hình hóa thống nhất hoặc UML theo cách sau

Kế thừa đa cấp có được hỗ trợ trong python không?

Các lớp được biểu diễn dưới dạng các hộp có tên lớp ở trên cùng. Mối quan hệ thừa kế được biểu diễn bằng mũi tên từ lớp dẫn xuất trỏ đến lớp cơ sở. Từ kéo dài thường được thêm vào mũi tên

Ghi chú. Trong quan hệ thừa kế

  • Các lớp kế thừa từ một lớp khác được gọi là lớp dẫn xuất, lớp con hoặc kiểu con
  • Các lớp mà các lớp khác được dẫn xuất từ ​​đó được gọi là các lớp cơ sở hoặc siêu lớp
  • Một lớp dẫn xuất được cho là kế thừa, kế thừa hoặc mở rộng một lớp cơ sở

Giả sử bạn có một lớp cơ sở

# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
0 và bạn lấy từ nó để tạo một lớp
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
1. Mối quan hệ thừa kế nói rằng một
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
1 là một
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
0. Điều này có nghĩa là
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
1 kế thừa giao diện và triển khai của
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
0 và các đối tượng
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
1 có thể được sử dụng để thay thế các đối tượng
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
0 trong ứng dụng

Điều này được gọi là nguyên tắc thay thế Liskov. Nguyên tắc nói rằng “trong một chương trình máy tính, nếu

# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
8 là một kiểu con của
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
9, thì các đối tượng kiểu
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
9 có thể được thay thế bằng các đối tượng kiểu
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
8 mà không làm thay đổi bất kỳ thuộc tính mong muốn nào của chương trình”

Trong bài viết này, bạn sẽ thấy lý do tại sao bạn phải luôn tuân theo nguyên tắc thay thế Liskov khi tạo hệ thống phân cấp lớp của mình và những vấn đề bạn sẽ gặp phải nếu không tuân theo

Thành phần là gì?

Thành phần là một khái niệm mà các mô hình có một mối quan hệ. Nó cho phép tạo các kiểu phức tạp bằng cách kết hợp các đối tượng thuộc các kiểu khác. This means that a class

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
102 can contain an object of another class
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
103. This relationship means that a
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
102 has a
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
103

UML represents composition as follows

Kế thừa đa cấp có được hỗ trợ trong python không?

Composition is represented through a line with a diamond at the composite class pointing to the component class. The composite side can express the cardinality of the relationship. The cardinality indicates the number or valid range of

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
103 instances the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
102 class will contain

In the diagram above, the

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
108 represents that the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
102 class contains one object of type
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
103. Cardinality can be expressed in the following ways

  • A number indicates the number of
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    103 instances that are contained in the
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    102
  • The * symbol indicates that the
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    102 class can contain a variable number of
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    103 instances
  • A range 1. 4 indicates that the
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    102 class can contain a range of
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    103 instances. Phạm vi được chỉ định với số lượng phiên bản tối thiểu và tối đa hoặc tối thiểu và nhiều phiên bản như trong 1. *

Note. Classes that contain objects of other classes are usually referred to as composites, where classes that are used to create more complex types are referred to as components

For example, your

# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
1 class can be composed by another object of type
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
118. Composition allows you to express that relationship by saying a
# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
1 has a
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
118

Composition enables you to reuse code by adding objects to other objects, as opposed to inheriting the interface and implementation of other classes. Both

# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission
1 and
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
122 classes can leverage the functionality of
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
118 through composition without deriving one class from the other

An Overview of Inheritance in Python

Everything in Python is an object. Modules are objects, class definitions and functions are objects, and of course, objects created from classes are objects too

Inheritance is a required feature of every object oriented programming language. This means that Python supports inheritance, and as you’ll see later, it’s one of the few languages that supports multiple inheritance

When you write Python code using classes, you are using inheritance even if you don’t know you’re using it. Let’s take a look at what that means

Loại bỏ các quảng cáo

The Object Super Class

The easiest way to see inheritance in Python is to jump into the Python interactive shell and write a little bit of code. You’ll start by writing the simplest class possible

>>>

Eating...
Barking...
Weeping
3

You declared a class

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
124 that doesn’t do much, but it will illustrate the most basic inheritance concepts. Now that you have the class declared, you can use the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
125 function to list its members

>>>

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
1

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
125 returns a list of all the members in the specified object. You have not declared any members in
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
124, so where is the list coming from? You can find out using the interactive interpreter

>>>

Eating...
Barking...
Weeping
9

As you can see, the two lists are nearly identical. There are some additional members in

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
124 like
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
129 and
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
130, but every single member of the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
131 class is also present in
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
124

Điều này là do mọi lớp bạn tạo trong Python đều bắt nguồn hoàn toàn từ

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
131. Bạn có thể viết rõ ràng hơn và viết
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
134, nhưng nó thừa và không cần thiết

Ghi chú. Trong Python 2, bạn phải bắt nguồn rõ ràng từ

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
131 vì những lý do nằm ngoài phạm vi của bài viết này, nhưng bạn có thể đọc về nó trong phần Kiểu mới và lớp cổ điển của tài liệu Python 2

Ngoại lệ là một ngoại lệ

Mỗi lớp mà bạn tạo bằng Python sẽ ngầm định xuất phát từ

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
131. Ngoại lệ đối với quy tắc này là các lớp được sử dụng để chỉ ra lỗi bằng cách đưa ra một ngoại lệ

Bạn có thể thấy sự cố khi sử dụng trình thông dịch tương tác Python

>>>

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
9

Bạn đã tạo một lớp mới để chỉ ra một loại lỗi. Sau đó, bạn đã cố gắng sử dụng nó để đưa ra một ngoại lệ. Một ngoại lệ được đưa ra nhưng đầu ra cho biết rằng ngoại lệ đó thuộc loại

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
137 không phải
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
138 và tất cả
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
139

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
140 là lớp cơ sở được cung cấp cho tất cả các loại lỗi. Để tạo một loại lỗi mới, bạn phải dẫn xuất lớp của mình từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
140 hoặc một trong các lớp dẫn xuất của nó. Quy ước trong Python là lấy các loại lỗi tùy chỉnh của bạn từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
142, từ đó dẫn xuất từ ​​
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
140

Cách chính xác để xác định loại lỗi của bạn là như sau

>>>

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
7

Như bạn có thể thấy, khi bạn nâng cao

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
138, đầu ra sẽ hiển thị chính xác loại lỗi được đưa ra

Tạo phân cấp lớp

Kế thừa là cơ chế bạn sẽ sử dụng để tạo phân cấp của các lớp liên quan. Các lớp liên quan này sẽ chia sẻ một giao diện chung sẽ được định nghĩa trong các lớp cơ sở. Các lớp dẫn xuất có thể chuyên biệt hóa giao diện bằng cách cung cấp một triển khai cụ thể khi áp dụng

Trong phần này, bạn sẽ bắt đầu lập mô hình hệ thống nhân sự. Ví dụ này sẽ chứng minh việc sử dụng tính kế thừa và cách các lớp dẫn xuất có thể cung cấp triển khai cụ thể của giao diện lớp cơ sở

Hệ thống nhân sự cần xử lý bảng lương cho nhân viên của công ty, nhưng có nhiều loại nhân viên khác nhau tùy thuộc vào cách tính lương của họ

Bạn bắt đầu bằng cách triển khai lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 xử lý bảng lương

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
0

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 triển khai phương pháp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 lấy một tập hợp nhân viên và in
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148,
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149 của họ và kiểm tra số lượng bằng cách sử dụng phương pháp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 hiển thị trên từng đối tượng nhân viên

Bây giờ, bạn triển khai lớp cơ sở

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 xử lý giao diện chung cho mọi loại nhân viên

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
7

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 là lớp cơ sở cho tất cả các loại nhân viên. Nó được xây dựng với một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 và một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149. Những gì bạn đang nói là mỗi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 phải được gán một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 cũng như một tên

The HR system requires that every

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 processed must provide a
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 interface that returns the weekly salary for the employee. Việc triển khai giao diện đó khác nhau tùy thuộc vào loại
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

Ví dụ, nhân viên hành chính có mức lương cố định, vì vậy hàng tuần họ được trả số tiền như nhau

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
6

Bạn tạo một lớp dẫn xuất

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
160 kế thừa
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151. Lớp được khởi tạo với
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149 theo yêu cầu của lớp cơ sở và bạn sử dụng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
164 để khởi tạo các thành viên của lớp cơ sở. Bạn có thể đọc tất cả về
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
164 trong Supercharge Your Classs With Python super()

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
160 cũng yêu cầu tham số khởi tạo
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
167 đại diện cho số tiền mà nhân viên kiếm được mỗi tuần

Lớp cung cấp phương pháp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 bắt buộc được sử dụng bởi hệ thống nhân sự. Việc triển khai chỉ trả về số tiền được lưu trữ trong
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
167

Công ty cũng sử dụng công nhân sản xuất được trả lương theo giờ, vì vậy bạn thêm

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
170 vào hệ thống nhân sự

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate

Lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
170 được khởi tạo với
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149, giống như lớp cơ sở, cộng với
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
174 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
175 cần thiết để tính bảng lương. Phương thức
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 được triển khai bằng cách trả về số giờ đã làm việc nhân với tỷ lệ giờ

Cuối cùng, công ty tuyển dụng các cộng tác viên bán hàng được trả lương cố định cộng với hoa hồng dựa trên doanh số bán hàng của họ, vì vậy bạn tạo một lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
177

# In hr.py

class CommissionEmployee(SalaryEmployee):
    def __init__(self, id, name, weekly_salary, commission):
        super().__init__(id, name, weekly_salary)
        self.commission = commission

    def calculate_payroll(self):
        fixed = super().calculate_payroll()
        return fixed + self.commission

Bạn lấy được

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
177 từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
160 vì cả hai lớp đều có một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
167 để xem xét. Đồng thời,
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
177 được khởi tạo với giá trị
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
182 dựa trên doanh số bán hàng cho nhân viên

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 tận dụng việc triển khai lớp cơ sở để truy xuất mức lương của
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
184 và thêm giá trị hoa hồng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
177 bắt nguồn từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
160, nên bạn có quyền truy cập trực tiếp vào thuộc tính
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
167 và bạn có thể triển khai
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 bằng cách sử dụng giá trị của thuộc tính đó

Vấn đề với việc truy cập trực tiếp thuộc tính là nếu việc triển khai

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
189 thay đổi, thì bạn cũng sẽ phải thay đổi việc triển khai
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
190. It’s better to rely on the already implemented method in the base class and extend the functionality as needed

Bạn đã tạo hệ thống phân cấp lớp đầu tiên của mình cho hệ thống. Sơ đồ UML của các lớp trông như thế này

Kế thừa đa cấp có được hỗ trợ trong python không?

Sơ đồ cho thấy hệ thống phân cấp thừa kế của các lớp. Các lớp dẫn xuất triển khai giao diện

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
191, được yêu cầu bởi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145. Việc triển khai
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
193 yêu cầu các đối tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
194 được thông qua có chứa một triển khai
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148,
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
197

Các giao diện được biểu diễn tương tự như các lớp với từ giao diện phía trên tên giao diện. Tên giao diện thường được bắt đầu bằng chữ hoa

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
198

Ứng dụng tạo nhân viên của mình và chuyển họ đến hệ thống tính lương để xử lý bảng lương

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
10

Bạn có thể chạy chương trình trong dòng lệnh và xem kết quả

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
11

Chương trình tạo ba đối tượng nhân viên, một đối tượng cho mỗi lớp dẫn xuất. Sau đó, nó tạo hệ thống tính lương và chuyển danh sách nhân viên sang phương thức

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 của nó, tính toán bảng lương cho từng nhân viên và in kết quả

Lưu ý cách lớp cơ sở

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 không định nghĩa phương thức
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147. Điều này có nghĩa là nếu bạn định tạo một đối tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 đơn giản và chuyển nó tới
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145, thì bạn sẽ gặp lỗi. You can try it in the Python interactive interpreter

>>>

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
12

Mặc dù bạn có thể khởi tạo một đối tượng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 nhưng đối tượng đó không thể được sử dụng bởi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145. Tại sao? . To meet the requirements of
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145, you’ll want to convert the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 class, which is currently a concrete class, to an abstract class. Bằng cách đó, không có nhân viên nào chỉ là một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151, mà là một người thực hiện
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147

Loại bỏ các quảng cáo

Abstract Base Classes in Python

Lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 trong ví dụ trên được gọi là lớp cơ sở trừu tượng. Các lớp cơ sở trừu tượng tồn tại để được kế thừa, nhưng không bao giờ được khởi tạo. Python cung cấp mô-đun
Eating...
Barking...
Weeping
913 để định nghĩa các lớp cơ sở trừu tượng

Bạn có thể sử dụng dấu gạch dưới hàng đầu trong tên lớp của mình để thông báo rằng các đối tượng của lớp đó không nên được tạo. Underscores provide a friendly way to prevent misuse of your code, but they don’t prevent eager users from creating instances of that class

The

Eating...
Barking...
Weeping
913 module in the Python standard library provides functionality to prevent creating objects from abstract base classes

You can modify the implementation of the

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 class to ensure that it can’t be instantiated

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
13

You derive

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 from
Eating...
Barking...
Weeping
917, making it an abstract base class. Then, you decorate the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 method with the
Eating...
Barking...
Weeping
919 decorator

This change has two nice side-effects

  1. You’re telling users of the module that objects of type
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    151 can’t be created
  2. You’re telling other developers working on the
    Eating...
    Barking...
    Weeping
    
    921 module that if they derive from
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    151, then they must override the
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    147 abstract method

You can see that objects of type

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 can’t be created using the interactive interpreter

>>>

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
14

Đầu ra cho thấy rằng lớp không thể được khởi tạo vì nó chứa một phương thức trừu tượng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
197. Các lớp dẫn xuất phải ghi đè phương thức để cho phép tạo các đối tượng thuộc loại của chúng

Kế thừa triển khai so với Kế thừa giao diện

Khi bạn dẫn xuất một lớp này từ một lớp khác, lớp dẫn xuất sẽ kế thừa cả hai

  1. Giao diện lớp cơ sở. Lớp dẫn xuất kế thừa tất cả các phương thức, thuộc tính và thuộc tính của lớp cơ sở

  2. Việc thực hiện lớp cơ sở. Lớp dẫn xuất kế thừa mã cài đặt giao diện lớp

Hầu hết thời gian, bạn sẽ muốn kế thừa việc triển khai một lớp, nhưng bạn sẽ muốn triển khai nhiều giao diện, để các đối tượng của bạn có thể được sử dụng trong các tình huống khác nhau

Các ngôn ngữ lập trình hiện đại được thiết kế dựa trên khái niệm cơ bản này. Chúng cho phép bạn kế thừa từ một lớp duy nhất, nhưng bạn có thể triển khai nhiều giao diện

Trong Python, bạn không cần phải khai báo giao diện một cách rõ ràng. Bất kỳ đối tượng nào triển khai giao diện mong muốn đều có thể được sử dụng thay cho đối tượng khác. Điều này được gọi là gõ vịt. Gõ vịt thường được giải thích là “nếu nó hoạt động giống như một con vịt, thì đó là một con vịt. ”

Để minh họa điều này, bây giờ bạn sẽ thêm một lớp

Eating...
Barking...
Weeping
926 vào ví dụ trên, lớp này không xuất phát từ lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
15

Lớp

Eating...
Barking...
Weeping
926 không xuất phát từ lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151, nhưng nó hiển thị cùng một giao diện được yêu cầu bởi lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145.
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
193 yêu cầu một danh sách các đối tượng triển khai giao diện sau

  • Thuộc tính hoặc thuộc tính
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    148 trả về id của nhân viên
  • Thuộc tính hoặc thuộc tính
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    149 đại diện cho tên của nhân viên
  • Phương thức
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    147 không nhận bất kỳ tham số nào và trả về số tiền lương để xử lý

Tất cả các yêu cầu này đều được lớp

Eating...
Barking...
Weeping
926 đáp ứng, vì vậy lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 vẫn có thể tính toán bảng lương của mình

Bạn có thể sửa đổi chương trình để sử dụng lớp

Eating...
Barking...
Weeping
926

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
16

Chương trình tạo một đối tượng

Eating...
Barking...
Weeping
926 và thêm nó vào danh sách được xử lý bởi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145. Bây giờ bạn có thể chạy chương trình và xem đầu ra của nó

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
17

Như bạn thấy,

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 vẫn có thể xử lý đối tượng mới vì nó đáp ứng giao diện mong muốn

Vì bạn không cần phải xuất phát từ một lớp cụ thể để chương trình có thể sử dụng lại các đối tượng của bạn, bạn có thể hỏi tại sao bạn nên sử dụng tính kế thừa thay vì chỉ triển khai giao diện mong muốn. Các quy tắc sau đây có thể giúp bạn

  • Sử dụng kế thừa để sử dụng lại một triển khai. Các lớp dẫn xuất của bạn nên tận dụng hầu hết việc triển khai lớp cơ sở của chúng. Họ cũng phải mô hình hóa một mối quan hệ. Một lớp ________ 4941 cũng có thể có một

    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    148 và một
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    149, nhưng một ________ 4941 không phải là một
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    151, vì vậy bạn không nên sử dụng thừa kế

  • Thực hiện một giao diện được sử dụng lại. Khi bạn muốn lớp của mình được sử dụng lại bởi một phần cụ thể của ứng dụng, bạn triển khai giao diện bắt buộc trong lớp của mình, nhưng bạn không cần cung cấp lớp cơ sở hoặc kế thừa từ lớp khác

Bây giờ bạn có thể làm sạch ví dụ trên để chuyển sang chủ đề tiếp theo. Bạn có thể xóa tệp

Eating...
Barking...
Weeping
946 rồi sửa đổi mô-đun
Eating...
Barking...
Weeping
921 về trạng thái ban đầu

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
18

Bạn đã loại bỏ việc nhập mô-đun

Eating...
Barking...
Weeping
913 vì lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 không cần phải trừu tượng. Bạn cũng đã xóa phương thức trừu tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
197 khỏi nó vì nó không cung cấp bất kỳ triển khai nào

Về cơ bản, bạn đang kế thừa việc triển khai các thuộc tính

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149 của lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 trong các lớp dẫn xuất của bạn. Vì
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 chỉ là một giao diện của phương thức
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
193, nên bạn không cần triển khai nó trong lớp cơ sở
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

Lưu ý cách lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
177 bắt nguồn từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
160. Điều này có nghĩa là
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
177 kế thừa triển khai và giao diện của
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
160. Bạn có thể thấy cách phương thức
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
190 tận dụng việc triển khai lớp cơ sở vì nó dựa vào kết quả từ
Eating...
Barking...
Weeping
962 để triển khai phiên bản của chính nó

Loại bỏ các quảng cáo

Vấn đề bùng nổ lớp học

Nếu bạn không cẩn thận, tính kế thừa có thể dẫn bạn đến một cấu trúc phân cấp khổng lồ của các lớp khó hiểu và khó duy trì. Đây được gọi là vấn đề bùng nổ lớp

Bạn đã bắt đầu xây dựng hệ thống phân cấp lớp gồm các loại

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 được sử dụng bởi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 để tính bảng lương. Bây giờ, bạn cần thêm một số chức năng cho các lớp đó để chúng có thể được sử dụng với
Eating...
Barking...
Weeping
965 mới

Eating...
Barking...
Weeping
965 theo dõi năng suất dựa trên vai trò của nhân viên. Có nhiều vai trò nhân viên khác nhau

  • quản lý. Họ đi xung quanh và la mắng mọi người bảo họ phải làm gì. Họ là những người làm công ăn lương và kiếm được nhiều tiền hơn
  • thư ký. They do all the paper work for managers and ensure that everything gets billed and payed on time. Họ cũng là người làm công ăn lương nhưng kiếm được ít tiền hơn
  • nhân viên kinh doanh. Họ thực hiện rất nhiều cuộc điện thoại để bán sản phẩm. Họ có lương, nhưng họ cũng nhận được hoa hồng khi bán hàng
  • Công nhân nhà máy. Họ sản xuất các sản phẩm cho công ty. Họ được trả lương theo giờ

Với những yêu cầu đó, bạn bắt đầu thấy rằng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 và các lớp dẫn xuất của nó có thể thuộc về một nơi nào đó khác với mô-đun
Eating...
Barking...
Weeping
921 bởi vì bây giờ chúng cũng được sử dụng bởi
Eating...
Barking...
Weeping
965

Bạn tạo một mô-đun

Eating...
Barking...
Weeping
970 và di chuyển các lớp đến đó

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
19

Việc triển khai vẫn giữ nguyên, nhưng bạn chuyển các lớp sang mô-đun

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
194. Bây giờ, bạn thay đổi chương trình của mình để hỗ trợ thay đổi

Eating...
Barking...
Weeping
90

Bạn chạy chương trình và xác minh rằng nó vẫn hoạt động

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
11

Với mọi thứ đã sẵn sàng, bạn bắt đầu thêm các lớp mới

Eating...
Barking...
Weeping
92

Đầu tiên, bạn thêm một lớp

Eating...
Barking...
Weeping
972 bắt nguồn từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
160. Lớp hiển thị một phương thức
Eating...
Barking...
Weeping
974 sẽ được sử dụng bởi hệ thống năng suất. Phương pháp lấy
Eating...
Barking...
Weeping
975 nhân viên đã làm việc

Sau đó, bạn thêm

Eating...
Barking...
Weeping
976,
Eating...
Barking...
Weeping
977 và
Eating...
Barking...
Weeping
978 rồi triển khai giao diện
Eating...
Barking...
Weeping
974 để hệ thống năng suất có thể sử dụng chúng

Bây giờ, bạn có thể thêm lớp

Eating...
Barking...
Weeping
980

Eating...
Barking...
Weeping
93

Lớp theo dõi nhân viên theo phương pháp

Eating...
Barking...
Weeping
981 lấy danh sách nhân viên và số giờ để theo dõi. Bây giờ bạn có thể thêm hệ thống năng suất vào chương trình của mình

Eating...
Barking...
Weeping
94

Chương trình tạo danh sách nhân viên các loại. Danh sách nhân viên được gửi đến hệ thống năng suất để theo dõi công việc của họ trong 40 giờ. Sau đó, cùng một danh sách nhân viên được gửi đến hệ thống bảng lương để tính lương cho họ

Bạn có thể chạy chương trình để xem đầu ra

Eating...
Barking...
Weeping
95

Chương trình hiển thị các nhân viên làm việc trong 40 giờ thông qua hệ thống năng suất. Sau đó, nó tính toán và hiển thị bảng lương cho từng nhân viên

Chương trình hoạt động như mong đợi, nhưng bạn phải thêm bốn lớp mới để hỗ trợ các thay đổi. Khi có các yêu cầu mới, hệ thống phân cấp lớp của bạn chắc chắn sẽ phát triển, dẫn đến vấn đề bùng nổ lớp trong đó hệ thống phân cấp của bạn sẽ trở nên lớn đến mức chúng sẽ khó hiểu và khó duy trì

Sơ đồ sau đây cho thấy hệ thống phân cấp lớp mới

Kế thừa đa cấp có được hỗ trợ trong python không?

Sơ đồ cho thấy hệ thống phân cấp lớp đang phát triển như thế nào. Các yêu cầu bổ sung có thể có tác động theo cấp số nhân về số lượng lớp học với thiết kế này

Loại bỏ các quảng cáo

Kế thừa nhiều lớp

Python là một trong số ít ngôn ngữ lập trình hiện đại hỗ trợ đa kế thừa. Đa kế thừa là khả năng dẫn xuất một lớp từ nhiều lớp cơ sở cùng một lúc

Đa kế thừa có tiếng xấu đến mức hầu hết các ngôn ngữ lập trình hiện đại không hỗ trợ nó. Thay vào đó, các ngôn ngữ lập trình hiện đại hỗ trợ khái niệm giao diện. Trong các ngôn ngữ đó, bạn kế thừa từ một lớp cơ sở duy nhất và sau đó triển khai nhiều giao diện, vì vậy lớp của bạn có thể được sử dụng lại trong các tình huống khác nhau

Cách tiếp cận này đặt ra một số ràng buộc trong thiết kế của bạn. Bạn chỉ có thể kế thừa việc triển khai một lớp bằng cách xuất phát trực tiếp từ nó. Bạn có thể triển khai nhiều giao diện, nhưng bạn không thể kế thừa việc triển khai nhiều lớp

Ràng buộc này tốt cho thiết kế phần mềm vì nó buộc bạn phải thiết kế các lớp của mình với ít phụ thuộc lẫn nhau hơn. Ở phần sau của bài viết này, bạn sẽ thấy rằng bạn có thể tận dụng nhiều triển khai thông qua thành phần, giúp phần mềm trở nên linh hoạt hơn. Tuy nhiên, phần này nói về đa thừa kế, vì vậy chúng ta hãy xem nó hoạt động như thế nào

Hóa ra đôi khi thư ký tạm thời được thuê khi có quá nhiều giấy tờ phải làm. Lớp

Eating...
Barking...
Weeping
982 thực hiện vai trò của lớp
Eating...
Barking...
Weeping
976 trong ngữ cảnh của lớp
Eating...
Barking...
Weeping
965, nhưng đối với mục đích tính lương, nó là lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
170

Bạn nhìn vào thiết kế lớp học của bạn. Nó đã phát triển một chút, nhưng bạn vẫn có thể hiểu nó hoạt động như thế nào. Có vẻ như bạn có hai lựa chọn

  1. Xuất phát từ

    Eating...
    Barking...
    Weeping
    
    976. Bạn có thể bắt nguồn từ ________ 4976 để kế thừa phương thức ________ 4988 cho vai trò, sau đó ghi đè phương thức
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    147 để triển khai nó dưới dạng
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    170

  2. Xuất phát từ

    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    170. Bạn có thể bắt nguồn từ
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    170 để kế thừa phương thức
    Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    147, sau đó ghi đè phương thức
    Eating...
    Barking...
    Weeping
    
    988 để triển khai nó dưới dạng
    Eating...
    Barking...
    Weeping
    
    976

Sau đó, bạn nhớ rằng Python hỗ trợ đa kế thừa, vì vậy bạn quyết định lấy từ cả

Eating...
Barking...
Weeping
976 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
170

Eating...
Barking...
Weeping
96

Python allows you to inherit from two different classes by specifying them between parenthesis in the class declaration

Bây giờ, bạn sửa đổi chương trình của mình để thêm nhân viên thư ký tạm thời mới

Eating...
Barking...
Weeping
97

Bạn chạy chương trình để kiểm tra

Eating...
Barking...
Weeping
98

Bạn nhận được một ngoại lệ

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
137 nói rằng
Eating...
Barking...
Weeping
999 đối số vị trí được mong đợi, nhưng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
900 đã được đưa ra

Điều này là do bạn bắt nguồn

Eating...
Barking...
Weeping
982 đầu tiên từ
Eating...
Barking...
Weeping
976 và sau đó từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
170, vì vậy trình thông dịch đang cố gắng sử dụng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
904 để khởi tạo đối tượng

Được rồi, hãy đảo ngược nó

Eating...
Barking...
Weeping
99

Bây giờ, hãy chạy lại chương trình và xem điều gì sẽ xảy ra

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
90

Bây giờ, có vẻ như bạn đang thiếu một tham số

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
167, tham số này cần thiết để khởi tạo
Eating...
Barking...
Weeping
976, nhưng tham số đó không có ý nghĩa trong ngữ cảnh của một
Eating...
Barking...
Weeping
982 vì nó là một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
170

Có thể triển khai

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
909 sẽ giúp ích

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
91

Thử nó

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
92

Điều đó cũng không hiệu quả. Được rồi, đã đến lúc bạn đi sâu vào trình tự giải quyết phương pháp Python (MRO) để xem điều gì đang xảy ra

Khi một phương thức hoặc thuộc tính của một lớp được truy cập, Python sử dụng lớp MRO để tìm nó. MRO cũng được sử dụng bởi

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
164 để xác định phương thức hoặc thuộc tính nào sẽ gọi. Bạn có thể tìm hiểu thêm về
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
164 trong Supercharge Your Classs With Python super()

Bạn có thể đánh giá MRO lớp

Eating...
Barking...
Weeping
982 bằng trình thông dịch tương tác

>>>

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
93

MRO hiển thị thứ tự mà Python sẽ tìm kiếm một thuộc tính hoặc phương thức phù hợp. Trong ví dụ này, đây là điều xảy ra khi chúng ta tạo đối tượng

Eating...
Barking...
Weeping
982

  1. Phương pháp

    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    914 được gọi là

  2. Cuộc gọi

    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    915 phù hợp với
    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    916

  3. Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    170 gọi
    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    918, mà MRO sẽ khớp với
    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    904, được kế thừa từ
    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    920

Bởi vì các thông số không khớp, một ngoại lệ

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
137 được đưa ra

Bạn có thể bỏ qua MRO bằng cách đảo ngược thứ tự thừa kế và gọi trực tiếp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
922 như sau

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
94

Điều đó giải quyết vấn đề tạo đối tượng, nhưng bạn sẽ gặp phải vấn đề tương tự khi cố tính bảng lương. Bạn có thể chạy chương trình để xem vấn đề

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
95

Vấn đề bây giờ là vì bạn đã đảo ngược thứ tự thừa kế, MRO đang tìm phương thức

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
924 trước phương thức trong
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
170. Bạn cần ghi đè
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 trong
Eating...
Barking...
Weeping
982 và gọi triển khai đúng từ đó

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
96

Phương thức

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
197 gọi trực tiếp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
929 để đảm bảo rằng bạn nhận được kết quả chính xác. Bạn có thể chạy lại chương trình để thấy nó hoạt động

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
97

Chương trình hiện hoạt động như mong đợi vì bạn đang buộc thứ tự giải quyết phương thức bằng cách thông báo rõ ràng cho trình thông dịch biết phương pháp nào chúng tôi muốn sử dụng

Như bạn có thể thấy, đa thừa kế có thể gây nhầm lẫn, đặc biệt khi bạn gặp vấn đề về kim cương

Sơ đồ sau đây cho thấy vấn đề kim cương trong hệ thống phân cấp lớp học của bạn

Kế thừa đa cấp có được hỗ trợ trong python không?

Sơ đồ cho thấy vấn đề kim cương với thiết kế lớp hiện tại.

Eating...
Barking...
Weeping
982 sử dụng đa kế thừa để dẫn xuất từ ​​hai lớp mà cuối cùng cũng dẫn xuất từ ​​
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151. Điều này khiến hai đường dẫn đến lớp cơ sở
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151, đây là điều bạn muốn tránh trong thiết kế của mình

Vấn đề kim cương xuất hiện khi bạn đang sử dụng nhiều kế thừa và xuất phát từ hai lớp có lớp cơ sở chung. Điều này có thể gây ra phiên bản sai của một phương thức được gọi

Như bạn đã thấy, Python cung cấp một cách để buộc gọi đúng phương thức và việc phân tích MRO có thể giúp bạn hiểu vấn đề

Tuy nhiên, khi bạn gặp vấn đề về kim cương, tốt hơn hết là bạn nên suy nghĩ lại về thiết kế. Bây giờ bạn sẽ thực hiện một số thay đổi để tận dụng đa kế thừa, tránh vấn đề kim cương

Các lớp dẫn xuất

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 được sử dụng bởi hai hệ thống khác nhau

  1. Hệ thống năng suất theo dõi năng suất của nhân viên

  2. Hệ thống bảng lương tính toán bảng lương của nhân viên

Điều này có nghĩa là mọi thứ liên quan đến năng suất phải được đặt cùng nhau trong một mô-đun và mọi thứ liên quan đến bảng lương phải được đặt cùng nhau trong một mô-đun khác. Bạn có thể bắt đầu thay đổi mô-đun năng suất

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
98

Mô-đun

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
934 triển khai lớp
Eating...
Barking...
Weeping
965, cũng như các vai trò liên quan mà nó hỗ trợ. Các lớp triển khai giao diện
Eating...
Barking...
Weeping
974 theo yêu cầu của hệ thống, nhưng chúng không bắt nguồn từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

You can do the same with the

Eating...
Barking...
Weeping
921 module

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
99

The

Eating...
Barking...
Weeping
921 module implements the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145, which calculates payroll for the employees. It also implements the policy classes for payroll. As you can see, the policy classes don’t derive from
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 anymore

You can now add the necessary classes to the

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
194 module

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
70

The

Eating...
Barking...
Weeping
970 module imports policies and roles from the other modules and implements the different
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 types. You are still using multiple inheritance to inherit the implementation of the salary policy classes and the productivity roles, but the implementation of each class only needs to deal with initialization

Notice that you still need to explicitly initialize the salary policies in the constructors. You probably saw that the initializations of

Eating...
Barking...
Weeping
972 and
Eating...
Barking...
Weeping
976 are identical. Also, the initializations of
Eating...
Barking...
Weeping
978 and
Eating...
Barking...
Weeping
982 are the same

You will not want to have this kind of code duplication in more complex designs, so you have to be careful when designing class hierarchies

Here’s the UML diagram for the new design

Kế thừa đa cấp có được hỗ trợ trong python không?

The diagram shows the relationships to define the

Eating...
Barking...
Weeping
976 and
Eating...
Barking...
Weeping
982 using multiple inheritance, but avoiding the diamond problem

You can run the program and see how it works

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
71

You’ve seen how inheritance and multiple inheritance work in Python. You can now explore the topic of composition

Loại bỏ các quảng cáo

Composition in Python

Composition is an object oriented design concept that models a has a relationship. In composition, a class known as composite contains an object of another class known to as component. In other words, a composite class has a component of another class

Composition allows composite classes to reuse the implementation of the components it contains. The composite class doesn’t inherit the component class interface, but it can leverage its implementation

The composition relation between two classes is considered loosely coupled. That means that changes to the component class rarely affect the composite class, and changes to the composite class never affect the component class

This provides better adaptability to change and allows applications to introduce new requirements without affecting existing code

When looking at two competing software designs, one based on inheritance and another based on composition, the composition solution usually is the most flexible. You can now look at how composition works

You’ve already used composition in our examples. If you look at the

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 class, you’ll see that it contains two attributes

  1. Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    148 to identify an employee
  2. Name: James
    Age: 16
    Gender: Male
    Class: 10th
    Enter the marks of the respective subjects
    Literature: 50
    Math: 60
    Biology: 55
    Physics: 46
    
    
    Name:  James
    Age:  16
    Gender:  Male
    Study in:  10th
    Total Marks:  211
    149 to contain the name of the employee

These two attributes are objects that the

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 class has. Therefore, you can say that an
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 has an
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 and has a name

Another attribute for an

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 might be an
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
72

You implemented a basic address class that contains the usual components for an address. You made the

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
959 attribute optional because not all addresses will have that component

You implemented

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
960 to provide a pretty representation of an
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958. You can see this implementation in the interactive interpreter

>>>

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
73

When you

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
962 the
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
963 variable, the special method
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
960 is invoked. Vì bạn đã quá tải phương thức để trả về một chuỗi được định dạng dưới dạng địa chỉ, nên bạn sẽ có một biểu diễn đẹp, dễ đọc. Operator and Function Overloading in Custom Python Classes gives a good overview of the special methods available in classes that can be implemented to customize the behavior of your objects

You can now add the

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 to the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 class through composition

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
74

You initialize the

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
963 attribute to
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
968 for now to make it optional, but by doing that, you can now assign an
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 to an
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151. Also notice that there is no reference in the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
194 module to the
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
972 module

Composition is a loosely coupled relationship that often doesn’t require the composite class to have knowledge of the component

Sơ đồ UML thể hiện mối quan hệ giữa

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 trông như thế này

Kế thừa đa cấp có được hỗ trợ trong python không?

The diagram shows the basic composition relationship between

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 and
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958

Bây giờ bạn có thể sửa đổi lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 để tận dụng thuộc tính
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
963 trong
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
75

Bạn kiểm tra xem đối tượng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
194 có địa chỉ không, nếu có thì in ra. Bây giờ bạn có thể sửa đổi chương trình để gán một số địa chỉ cho nhân viên

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
76

Bạn đã thêm một vài địa chỉ vào các đối tượng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
981 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
982. Khi bạn chạy chương trình, bạn sẽ thấy các địa chỉ được in

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
77

Lưu ý cách đầu ra bảng lương cho các đối tượng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
981 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
982 hiển thị địa chỉ nơi séc được gửi

The

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 class leverages the implementation of the
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 class without any knowledge of what an
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 object is or how it’s represented. Kiểu thiết kế này linh hoạt đến mức bạn có thể thay đổi lớp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 mà không ảnh hưởng đến lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

Loại bỏ các quảng cáo

Thiết kế linh hoạt với bố cục

Composition is more flexible than inheritance because it models a loosely coupled relationship. Các thay đổi đối với một lớp thành phần có ảnh hưởng tối thiểu hoặc không ảnh hưởng đến lớp tổng hợp. Thiết kế dựa trên thành phần phù hợp hơn để thay đổi

Bạn thay đổi hành vi bằng cách cung cấp các thành phần mới triển khai các hành vi đó thay vì thêm các lớp mới vào hệ thống phân cấp của bạn

Hãy xem ví dụ đa thừa kế ở trên. Hãy tưởng tượng chính sách tiền lương mới sẽ ảnh hưởng đến thiết kế như thế nào. Cố gắng hình dung hệ thống phân cấp lớp sẽ như thế nào nếu cần có vai trò mới. Như bạn đã thấy trước đây, việc phụ thuộc quá nhiều vào tính kế thừa có thể dẫn đến bùng nổ giai cấp

Vấn đề lớn nhất không phải là số lượng các lớp trong thiết kế của bạn nhiều như thế nào, mà là mối quan hệ giữa các lớp đó chặt chẽ đến mức nào. Các lớp liên kết chặt chẽ ảnh hưởng lẫn nhau khi các thay đổi được đưa ra

Trong phần này, bạn sẽ sử dụng bố cục để thực hiện một thiết kế tốt hơn mà vẫn phù hợp với yêu cầu của

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 và
Eating...
Barking...
Weeping
965

Bạn có thể bắt đầu bằng cách triển khai chức năng của

Eating...
Barking...
Weeping
965

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
78

Lớp

Eating...
Barking...
Weeping
965 xác định một số vai trò bằng cách sử dụng mã định danh chuỗi được ánh xạ tới lớp vai trò thực hiện vai trò đó. Nó hiển thị một phương thức
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
994, được cung cấp một định danh vai trò, trả về đối tượng loại vai trò. Nếu không tìm thấy vai trò, thì một ngoại lệ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
995 sẽ được đưa ra

Nó cũng hiển thị chức năng trước đó trong phương pháp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
996, trong đó đưa ra một danh sách nhân viên, nó theo dõi năng suất của những nhân viên đó

Bây giờ bạn có thể triển khai các lớp vai trò khác nhau

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
79

Mỗi vai trò bạn đã triển khai hiển thị một

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
997 lấy số lượng
Eating...
Barking...
Weeping
975 đã làm việc. Các phương thức trả về một chuỗi đại diện cho các nhiệm vụ

Các lớp vai trò độc lập với nhau, nhưng chúng hiển thị cùng một giao diện, vì vậy chúng có thể hoán đổi cho nhau. Sau này bạn sẽ thấy chúng được sử dụng như thế nào trong ứng dụng

Bây giờ, bạn có thể triển khai

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 cho ứng dụng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
00

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 lưu giữ cơ sở dữ liệu nội bộ về các chính sách trả lương cho từng nhân viên. Nó hiển thị một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
701, được cung cấp cho một nhân viên
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148, trả về chính sách trả lương của nó. Nếu một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 được chỉ định không tồn tại trong hệ thống, thì phương thức này sẽ tạo ra một ngoại lệ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
995

Việc triển khai

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 hoạt động giống như trước đây. Nó lấy danh sách nhân viên, tính bảng lương và in kết quả

Bây giờ bạn có thể thực hiện các lớp chính sách trả lương

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
01

Trước tiên, bạn triển khai một lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
706 đóng vai trò là lớp cơ sở cho tất cả các chính sách trả lương. Lớp này theo dõi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
174, phổ biến cho tất cả các chính sách trả lương

Các lớp chính sách khác bắt nguồn từ

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
706. Chúng tôi sử dụng tính kế thừa ở đây vì chúng tôi muốn tận dụng việc triển khai
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
706. Ngoài ra,
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
710,
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
711 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
712 là một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
706

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
710 được khởi tạo với giá trị
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
167 sau đó được sử dụng trong
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147.
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
711 được khởi tạo với
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
175 và triển khai
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 bằng cách tận dụng lớp cơ sở
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
174

Lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
712 bắt nguồn từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
710 vì nó muốn kế thừa việc triển khai của nó. Nó được khởi tạo với tham số
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
167, nhưng nó cũng yêu cầu tham số
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
724

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
724 được sử dụng để tính toán
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
726, được triển khai như một thuộc tính để tính toán khi được yêu cầu. Trong ví dụ này, chúng tôi giả định rằng một lần bán hàng diễn ra sau mỗi 5 giờ làm việc và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
726 là số lần bán hàng nhân với giá trị
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
724

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
712 triển khai phương pháp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 bằng cách trước tiên tận dụng việc triển khai trong
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
710 và sau đó thêm hoa hồng đã tính

Bây giờ bạn có thể thêm lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
732 để quản lý địa chỉ nhân viên

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
02

Lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
732 giữ một cơ sở dữ liệu nội bộ gồm các đối tượng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 cho mỗi nhân viên. Nó hiển thị một phương thức
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
735 trả về địa chỉ của nhân viên được chỉ định
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148. Nếu nhân viên
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 không tồn tại, thì nó sẽ tăng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
995

Việc triển khai lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 vẫn giống như trước đây

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
72

Lớp quản lý các thành phần địa chỉ và cung cấp một biểu diễn đẹp về địa chỉ

Cho đến nay, các lớp mới đã được mở rộng để hỗ trợ nhiều chức năng hơn, nhưng không có thay đổi đáng kể nào đối với thiết kế trước đó. Điều này sẽ thay đổi với thiết kế của mô-đun

Eating...
Barking...
Weeping
970 và các lớp của nó

Bạn có thể bắt đầu bằng cách triển khai một lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
741

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
04

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
741 theo dõi tất cả nhân viên trong công ty. Đối với mỗi nhân viên, nó theo dõi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148,
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
745. It has an instance of the
Eating...
Barking...
Weeping
965, the
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145, and the
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
732. Những trường hợp này được sử dụng để tạo nhân viên

Nó hiển thị thuộc tính

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
749 trả về danh sách nhân viên. Các đối tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 được tạo trong một phương thức nội bộ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
751. Lưu ý rằng bạn không có các loại lớp học
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 khác nhau. Bạn chỉ cần triển khai một lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 duy nhất

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
05

Lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 được khởi tạo với các thuộc tính
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148,
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
149 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
963. Nó cũng yêu cầu năng suất
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
745 cho nhân viên và chính sách
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
759

Lớp hiển thị một phương thức

Eating...
Barking...
Weeping
988 mất nhiều giờ làm việc. Phương pháp này trước tiên truy xuất
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
761 từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
745. Nói cách khác, nó ủy quyền cho đối tượng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
745 để thực hiện nhiệm vụ của mình

Theo cách tương tự, nó ủy quyền cho đối tượng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
759 để theo dõi công việc
Eating...
Barking...
Weeping
975.
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
759, như bạn đã thấy, sử dụng số giờ đó để tính bảng lương nếu cần

Sơ đồ sau đây cho thấy thiết kế thành phần được sử dụng

Kế thừa đa cấp có được hỗ trợ trong python không?

Sơ đồ cho thấy thiết kế của các chính sách dựa trên thành phần. Có một

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 duy nhất bao gồm các đối tượng dữ liệu khác như
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 và phụ thuộc vào giao diện
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
769 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
191 để ủy thác công việc. Có nhiều triển khai của các giao diện này

Bây giờ bạn có thể sử dụng thiết kế này trong chương trình của mình

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
06

Bạn có thể chạy chương trình để xem đầu ra của nó

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
07

Thiết kế này được gọi là thiết kế dựa trên chính sách, trong đó các lớp bao gồm các chính sách và chúng ủy quyền cho các chính sách đó để thực hiện công việc

Thiết kế dựa trên chính sách đã được giới thiệu trong cuốn sách Modern C++ Design và nó sử dụng siêu lập trình mẫu trong C++ để đạt được kết quả

Python không hỗ trợ các mẫu, nhưng bạn có thể đạt được kết quả tương tự bằng cách sử dụng bố cục, như bạn đã thấy trong ví dụ trên

Kiểu thiết kế này mang đến cho bạn tất cả sự linh hoạt mà bạn cần khi các yêu cầu thay đổi. Hãy tưởng tượng bạn cần thay đổi cách tính lương cho một đối tượng trong thời gian chạy

Loại bỏ các quảng cáo

Tùy chỉnh hành vi với thành phần

Nếu thiết kế của bạn dựa trên sự kế thừa, bạn cần tìm cách thay đổi loại đối tượng để thay đổi hành vi của nó. Với bố cục, bạn chỉ cần thay đổi chính sách mà đối tượng sử dụng

Hãy tưởng tượng rằng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
981 của chúng ta đột nhiên trở thành nhân viên tạm thời được trả lương theo giờ. Bạn có thể sửa đổi đối tượng trong quá trình thực hiện chương trình theo cách sau

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
08

Chương trình lấy danh sách nhân viên từ

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
741 và lấy nhân viên đầu tiên, đó là người quản lý mà chúng tôi muốn. Sau đó, nó tạo một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
711 mới được khởi tạo ở mức 55 đô la mỗi giờ và gán nó cho đối tượng người quản lý

Chính sách mới hiện được sử dụng bởi

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
145 sửa đổi hành vi hiện có. Bạn có thể chạy lại chương trình để xem kết quả

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
09

Tấm séc cho Mary Poppins, người quản lý của chúng tôi, hiện có giá 2200 đô la thay vì mức lương cố định 3000 đô la mà cô ấy có mỗi tuần

Lưu ý cách chúng tôi đã thêm quy tắc kinh doanh đó vào chương trình mà không thay đổi bất kỳ lớp hiện có nào. Xem xét loại thay đổi nào sẽ được yêu cầu với thiết kế kế thừa

Bạn sẽ phải tạo một lớp mới và thay đổi loại nhân viên quản lý. Không có khả năng bạn có thể thay đổi chính sách trong thời gian chạy

Lựa chọn giữa Kế thừa và Thành phần trong Python

Cho đến giờ, bạn đã thấy cách hoạt động của tính kế thừa và thành phần trong Python. Bạn đã thấy rằng các lớp dẫn xuất kế thừa giao diện và triển khai của các lớp cơ sở của chúng. Bạn cũng đã thấy rằng thành phần cho phép bạn sử dụng lại việc triển khai một lớp khác

Bạn đã triển khai hai giải pháp cho cùng một vấn đề. Giải pháp đầu tiên sử dụng đa kế thừa và giải pháp thứ hai sử dụng thành phần

Bạn cũng đã thấy rằng cách gõ vịt của Python cho phép bạn sử dụng lại các đối tượng với các phần hiện có của chương trình bằng cách triển khai giao diện mong muốn. Trong Python, không cần thiết phải xuất phát từ một lớp cơ sở để các lớp của bạn được sử dụng lại

Tại thời điểm này, bạn có thể hỏi khi nào nên sử dụng thừa kế so với thành phần trong Python. Cả hai đều cho phép sử dụng lại mã. Kế thừa và thành phần có thể giải quyết các vấn đề tương tự trong chương trình Python của bạn

Lời khuyên chung là sử dụng mối quan hệ tạo ra ít phụ thuộc hơn giữa hai lớp. Mối quan hệ này là thành phần. Tuy nhiên, sẽ có lúc việc kế thừa sẽ có ý nghĩa hơn

Các phần sau đây cung cấp một số hướng dẫn để giúp bạn đưa ra lựa chọn đúng đắn giữa kế thừa và thành phần trong Python

Kế thừa mô hình Mối quan hệ “Là A”

Kế thừa chỉ nên được sử dụng để mô hình hóa một mối quan hệ. Nguyên tắc thay thế của Liskov nói rằng một đối tượng kiểu

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
6, kế thừa từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
7, có thể thay thế một đối tượng kiểu
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
7 mà không làm thay đổi các thuộc tính mong muốn của chương trình

Liskov’s substitution principle is the most important guideline to determine if inheritance is the appropriate design solution. Tuy nhiên, câu trả lời có thể không đơn giản trong mọi tình huống. May mắn thay, có một bài kiểm tra đơn giản mà bạn có thể sử dụng để xác định xem thiết kế của mình có tuân theo nguyên tắc thay thế của Liskov hay không

Giả sử bạn có một lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
778 cung cấp cách triển khai và giao diện mà bạn muốn sử dụng lại trong một lớp khác
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
779. Suy nghĩ ban đầu của bạn là bạn có thể lấy
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
779 từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
778 và kế thừa cả giao diện và triển khai. Để chắc chắn đây là thiết kế phù hợp, bạn làm theo các bước sau

  1. Đánh giá

    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    779 là một
    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    778. Hãy suy nghĩ về mối quan hệ này và biện minh cho nó. Liệu nó có ý nghĩa?

  2. Đánh giá

    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    778 là một
    # In hr.py
    
    class HourlyEmployee(Employee):
        def __init__(self, id, name, hours_worked, hour_rate):
            super().__init__(id, name)
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll(self):
            return self.hours_worked * self.hour_rate
    
    779. Đảo ngược mối quan hệ và biện minh cho nó. Liệu nó cũng có ý nghĩa?

Nếu bạn có thể biện minh cho cả hai mối quan hệ, thì bạn không bao giờ nên kế thừa các lớp đó từ lớp khác. Hãy xem xét một ví dụ cụ thể hơn

Bạn có một lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 hiển thị một thuộc tính
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
787. Bạn cần một lớp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788, cũng có một lớp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
787. Có vẻ như
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 là một loại đặc biệt của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786, vì vậy có lẽ bạn có thể lấy từ nó và tận dụng cả giao diện và triển khai

Trước khi bắt đầu triển khai, bạn sử dụng nguyên tắc thay thế Liskov để đánh giá mối quan hệ

Một

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 là một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 vì diện tích của nó được tính từ tích của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
794 nhân với
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
795 của nó. Ràng buộc là
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
796 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
797 phải bằng nhau

Nó có ý nghĩa. Bạn có thể biện minh cho mối quan hệ và giải thích tại sao ________ 6788 lại là ________ 6786. Hãy đảo ngược mối quan hệ để xem nó có hợp lý không

Một

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 là một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 vì diện tích của nó được tính từ tích của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
794 nhân với
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
795 của nó. Sự khác biệt là
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
004 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
005 có thể thay đổi độc lập

Nó cũng có ý nghĩa. Bạn có thể biện minh cho mối quan hệ và mô tả các ràng buộc đặc biệt cho mỗi lớp. Đây là một dấu hiệu tốt rằng hai lớp này sẽ không bao giờ xuất phát từ nhau

Bạn có thể đã thấy các ví dụ khác dẫn xuất

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 để giải thích quyền thừa kế. Bạn có thể hoài nghi với bài kiểm tra nhỏ bạn vừa làm. Đủ công bằng. Hãy viết một chương trình minh họa vấn đề với việc lấy
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786

Đầu tiên, bạn triển khai

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786. Bạn thậm chí sẽ đóng gói các thuộc tính để đảm bảo rằng tất cả các ràng buộc đều được đáp ứng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
70

Lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 được khởi tạo với một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
795 và một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
794, và nó cung cấp một thuộc tính
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
787 trả về diện tích.
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
795 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
794 được đóng gói để tránh thay đổi chúng trực tiếp

Bây giờ, bạn lấy

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 và ghi đè giao diện cần thiết để đáp ứng các ràng buộc của một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
71

Lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 được khởi tạo với một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
021, được sử dụng để khởi tạo cả hai thành phần của lớp cơ sở. Bây giờ, bạn viết một chương trình nhỏ để kiểm tra hành vi

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
72

Chương trình tạo một

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 và một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 và khẳng định rằng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
787 của chúng được tính toán chính xác. Bạn có thể chạy chương trình và thấy rằng mọi thứ là
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
025 cho đến nay

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
73

Chương trình thực thi chính xác, vì vậy có vẻ như

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 chỉ là trường hợp đặc biệt của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786

Sau này, bạn cần hỗ trợ thay đổi kích thước đối tượng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786, vì vậy bạn thực hiện các thay đổi thích hợp cho lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
74

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
029 lấy
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
030 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
031 cho đối tượng. Bạn có thể thêm đoạn mã sau vào chương trình để xác minh rằng nó hoạt động chính xác

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
75

Bạn thay đổi kích thước đối tượng hình chữ nhật và xác nhận rằng khu vực mới là chính xác. Bạn có thể chạy chương trình để xác minh hành vi

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
73

The assertion passes, and you see that the program runs correctly

Vì vậy, điều gì sẽ xảy ra nếu bạn thay đổi kích thước một hình vuông?

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
77

Bạn chuyển các tham số tương tự cho

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
033 mà bạn đã sử dụng với
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
034 và in vùng. Khi bạn chạy chương trình, bạn sẽ thấy

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
78

Chương trình cho thấy khu vực mới là

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
035 giống như đối tượng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
034. Vấn đề bây giờ là đối tượng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
032 không còn đáp ứng ràng buộc của lớp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 rằng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
795 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
794 phải bằng nhau

Làm thế nào bạn có thể khắc phục vấn đề đó? . Bạn có thể ghi đè

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
029 trong
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
032 và bỏ qua tham số
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
794, nhưng điều đó sẽ gây nhầm lẫn cho những người đang xem các phần khác của chương trình nơi mà
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
044 đang được thay đổi kích thước và một số trong số chúng không nhận được các khu vực mong đợi vì chúng thực sự là
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
045

Trong một chương trình nhỏ như chương trình này, có thể dễ dàng phát hiện ra nguyên nhân của hành vi kỳ lạ, nhưng trong một chương trình phức tạp hơn, vấn đề sẽ khó tìm ra hơn

Thực tế là nếu bạn có thể biện minh cho mối quan hệ thừa kế giữa hai lớp theo cả hai cách, thì bạn không nên lấy lớp này từ lớp khác

Trong ví dụ này, không có nghĩa là

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 kế thừa giao diện và triển khai của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
029 từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786. Điều đó không có nghĩa là các đối tượng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 không thể thay đổi kích thước. Có nghĩa là giao diện khác vì nó chỉ cần tham số
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
021

Sự khác biệt trong giao diện này biện minh cho việc không xuất phát

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
788 từ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
786 như thử nghiệm ở trên đã khuyên

Loại bỏ các quảng cáo

Trộn các tính năng với các lớp Mixin

Một trong những cách sử dụng đa kế thừa trong Python là mở rộng các tính năng của lớp thông qua mixin. Mixin là một lớp cung cấp các phương thức cho các lớp khác nhưng không được coi là một lớp cơ sở

Mixin cho phép các lớp khác sử dụng lại giao diện và triển khai của nó mà không trở thành siêu lớp. Chúng triển khai một hành vi duy nhất có thể được tổng hợp cho các lớp không liên quan khác. Chúng tương tự như thành phần nhưng chúng tạo ra một mối quan hệ mạnh mẽ hơn

Giả sử bạn muốn chuyển đổi các đối tượng thuộc một số loại nhất định trong ứng dụng của mình thành biểu diễn từ điển của đối tượng. Bạn có thể cung cấp phương thức

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
053 trong mọi lớp mà bạn muốn hỗ trợ tính năng này, nhưng việc triển khai
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
053 có vẻ rất giống nhau

Đây có thể là một ứng cử viên sáng giá cho mixin. Bạn bắt đầu bằng cách sửa đổi một chút lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 từ ví dụ về thành phần

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
79

Sự thay đổi là rất nhỏ. Bạn vừa thay đổi thuộc tính

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
745 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
759 thành thuộc tính nội bộ bằng cách thêm dấu gạch dưới ở đầu vào tên của chúng. Bạn sẽ sớm thấy tại sao bạn lại thực hiện thay đổi đó

Bây giờ, bạn thêm lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
058

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
60

Lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
058 hiển thị phương thức
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
053 trả về biểu diễn của chính nó dưới dạng từ điển. Phương pháp này được triển khai dưới dạng hiểu
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
061 có nội dung: “Tạo ánh xạ từ điển
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
062 đến
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
063 cho mỗi mục trong
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
064 nếu
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
062 không phải là nội bộ. ”

Ghi chú. Đây là lý do tại sao chúng tôi tạo các thuộc tính vai trò và bảng lương bên trong lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151, vì chúng tôi không muốn trình bày chúng trong từ điển

Như bạn đã thấy ở phần đầu, việc tạo một lớp kế thừa một số thành viên từ

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
131 và một trong những thành viên đó là
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
129, về cơ bản là ánh xạ tất cả các thuộc tính trong một đối tượng với giá trị của chúng

Bạn duyệt qua tất cả các mục trong

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
129 và lọc ra những mục có tên bắt đầu bằng dấu gạch dưới bằng cách sử dụng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
070

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
071 kiểm tra giá trị được chỉ định. Nếu giá trị là một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
131, thì nó sẽ xem liệu nó cũng có thành viên
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
053 hay không và sử dụng nó để đại diện cho đối tượng. Mặt khác, nó trả về một biểu diễn chuỗi. Nếu giá trị không phải là
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
131, thì nó chỉ trả về giá trị

Bạn có thể sửa đổi lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 để hỗ trợ mixin này

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
61

Tất cả những gì bạn phải làm là kế thừa

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
058 để hỗ trợ chức năng. Sẽ rất tuyệt nếu hỗ trợ chức năng tương tự trong lớp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958, vì vậy thuộc tính
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
078 được biểu diễn theo cùng một cách

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
62

Bạn áp dụng mixin cho lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 để hỗ trợ tính năng. Bây giờ, bạn có thể viết một chương trình nhỏ để kiểm tra nó

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
63

Chương trình triển khai một

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
080 chuyển đổi từ điển thành chuỗi JSON bằng cách sử dụng thụt đầu dòng để đầu ra trông đẹp hơn

Sau đó, nó lặp qua tất cả các nhân viên, in ra biểu diễn từ điển do

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
053 cung cấp. Bạn có thể chạy chương trình để xem đầu ra của nó

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
64

Bạn đã tận dụng việc triển khai

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
058 trong cả hai lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 ngay cả khi chúng không liên quan. Bởi vì
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
058 chỉ cung cấp các hành vi, nó dễ dàng sử dụng lại với các lớp khác mà không gây ra vấn đề

Loại bỏ các quảng cáo

Thành phần cho Mô hình Mối quan hệ “Có A”

Các mô hình thành phần a có mối quan hệ. With composition, a class

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
102 has an instance of class
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
103 and can leverage its implementation. Lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
103 có thể được sử dụng lại trong các lớp khác hoàn toàn không liên quan đến lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
102

Trong ví dụ thành phần ở trên, lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 có một đối tượng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958.
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 thực hiện tất cả các chức năng để xử lý địa chỉ và nó có thể được sử dụng lại bởi các lớp khác

Các lớp khác như

Eating...
Barking...
Weeping
941 hoặc
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
094 có thể sử dụng lại
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 mà không liên quan đến
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151. Họ có thể tận dụng cùng một triển khai để đảm bảo rằng các địa chỉ được xử lý một cách nhất quán trên ứng dụng

Một vấn đề bạn có thể gặp phải khi sử dụng thành phần là một số lớp của bạn có thể bắt đầu phát triển bằng cách sử dụng nhiều thành phần. Các lớp của bạn có thể yêu cầu nhiều tham số trong hàm tạo chỉ để truyền vào các thành phần mà chúng được tạo thành. Điều này có thể làm cho các lớp học của bạn khó sử dụng

Một cách để tránh vấn đề là sử dụng Factory Method để xây dựng các đối tượng của bạn. Bạn đã làm điều đó với ví dụ thành phần

Nếu bạn nhìn vào việc triển khai lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
741, bạn sẽ nhận thấy rằng nó sử dụng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
751 để xây dựng một đối tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 với các tham số phù hợp

Thiết kế này sẽ hoạt động, nhưng lý tưởng nhất là bạn có thể xây dựng một đối tượng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 chỉ bằng cách chỉ định một
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148, ví dụ như
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
702

Những thay đổi sau đây có thể cải thiện thiết kế của bạn. Bạn có thể bắt đầu với mô-đun

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
934

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
65

Trước tiên, bạn tạo nội bộ lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
704, sau đó cung cấp biến nội bộ
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
705 cho mô-đun. Bạn đang thông báo với các nhà phát triển khác rằng họ không nên tạo hoặc sử dụng trực tiếp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
704. Thay vào đó, bạn cung cấp hai chức năng,
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
707 và
Eating...
Barking...
Weeping
981, làm giao diện chung cho mô-đun. Đây là những gì các mô-đun khác nên sử dụng

Điều bạn đang nói là

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
704 là một Singleton và chỉ nên có một đối tượng được tạo từ nó

Bây giờ, bạn có thể làm tương tự với mô-đun

Eating...
Barking...
Weeping
921

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
66

Một lần nữa, bạn tạo nội bộ

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
711 và cung cấp giao diện công cộng cho nó. Ứng dụng sẽ sử dụng giao diện công cộng để lấy chính sách và tính lương

Bây giờ bạn sẽ làm tương tự với mô-đun

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
972

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
67

Về cơ bản, bạn đang nói rằng chỉ nên có một

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
713, một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
711 và một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
704. Một lần nữa, mẫu thiết kế này được gọi là mẫu thiết kế Singleton, rất hữu ích cho các lớp chỉ nên có một thể hiện duy nhất.

Bây giờ, bạn có thể làm việc trên mô-đun

Eating...
Barking...
Weeping
970. Bạn cũng sẽ tạo một Singleton trong số
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
717, nhưng bạn sẽ thực hiện một số thay đổi bổ sung

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
68

Trước tiên, bạn nhập các hàm và lớp có liên quan từ các mô-đun khác.

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
717 được tạo nội bộ và ở dưới cùng, bạn tạo một phiên bản duy nhất. Phiên bản này là công khai và là một phần của giao diện vì bạn sẽ muốn sử dụng nó trong ứng dụng

Bạn đã thay đổi thuộc tính

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
719 thành một từ điển trong đó khóa là nhân viên
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 và giá trị là thông tin nhân viên. Bạn cũng đã sử dụng một phương thức
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
721 để trả lại thông tin cho nhân viên được chỉ định
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
722

Thuộc tính

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
723 hiện sắp xếp các khóa để trả lại cho nhân viên được sắp xếp theo
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 của họ. Bạn đã thay thế phương thức đã xây dựng các đối tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 bằng các lệnh gọi trực tiếp đến bộ khởi tạo
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

Lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 hiện được khởi tạo với
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 và sử dụng các hàm công khai có trong các mô-đun khác để khởi tạo các thuộc tính của nó

Bây giờ bạn có thể thay đổi chương trình để kiểm tra các thay đổi

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
69

Bạn nhập các hàm có liên quan từ các mô-đun

Eating...
Barking...
Weeping
921 và
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
934, cũng như lớp
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
731 và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151. Chương trình sạch hơn vì bạn đã hiển thị giao diện cần thiết và đóng gói cách các đối tượng được truy cập

Lưu ý rằng bây giờ bạn có thể tạo một đối tượng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 trực tiếp chỉ bằng cách sử dụng đối tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 của nó. Bạn có thể chạy chương trình để xem đầu ra của nó

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
0

Chương trình hoạt động giống như trước, nhưng bây giờ bạn có thể thấy rằng một đối tượng

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 duy nhất có thể được tạo từ
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
148 của nó và hiển thị biểu diễn từ điển của nó

Hãy xem kỹ lớp học

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
1

Lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 là một hỗn hợp chứa nhiều đối tượng cung cấp các chức năng khác nhau. Nó chứa một
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 thực hiện tất cả các chức năng liên quan đến nơi nhân viên sống

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 cũng chứa vai trò năng suất do mô-đun
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
934 cung cấp và chính sách lương do mô-đun
Eating...
Barking...
Weeping
921 cung cấp. Hai đối tượng này cung cấp các triển khai được sử dụng bởi lớp
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 để theo dõi công việc theo phương thức
Eating...
Barking...
Weeping
988 và để tính bảng lương theo phương thức
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147

Bạn đang sử dụng bố cục theo hai cách khác nhau. Lớp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
958 cung cấp dữ liệu bổ sung cho
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 trong đó các đối tượng vai trò và bảng lương cung cấp hành vi bổ sung

Tuy nhiên, mối quan hệ giữa

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151 và các đối tượng đó được kết hợp lỏng lẻo, cung cấp một số khả năng thú vị mà bạn sẽ thấy trong phần tiếp theo

Thành phần để thay đổi hành vi thời gian chạy

Kế thừa, trái ngược với thành phần, là một mối quan hệ cặp đôi chặt chẽ. Với tính kế thừa, chỉ có một cách để thay đổi và tùy chỉnh hành vi. Method overriding is the only way to customize the behavior of a base class. Điều này tạo ra những thiết kế cứng nhắc, khó thay đổi

Mặt khác, thành phần cung cấp một mối quan hệ được kết hợp lỏng lẻo cho phép các thiết kế linh hoạt và có thể được sử dụng để thay đổi hành vi trong thời gian chạy

Hãy tưởng tượng bạn cần hỗ trợ chính sách khuyết tật dài hạn (LTD) khi tính bảng lương. Chính sách quy định rằng một nhân viên tại LTD sẽ được trả 60% tiền lương hàng tuần của họ với giả định 40 giờ làm việc

Với thiết kế kế thừa, đây có thể là một yêu cầu rất khó hỗ trợ. Thêm nó vào ví dụ thành phần dễ dàng hơn nhiều. Hãy bắt đầu bằng cách thêm lớp chính sách

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
2

Lưu ý rằng

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
749 không kế thừa
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
706, nhưng triển khai cùng một giao diện. Điều này là do việc triển khai hoàn toàn khác, vì vậy chúng tôi không muốn kế thừa bất kỳ triển khai nào của
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
706

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
749 khởi tạo
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
753 thành
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
968 và cung cấp một phương thức
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
755 nội bộ sẽ đưa ra một ngoại lệ nếu
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
756 chưa được áp dụng. Sau đó, nó cung cấp một phương thức
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
757 để gán
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
753

Trước tiên, giao diện công khai kiểm tra xem

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
753 đã được áp dụng chưa, sau đó triển khai chức năng theo chính sách cơ sở đó. Phương thức
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
760 chỉ ủy quyền cho chính sách cơ sở và
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 sử dụng nó để tính toán
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
762 và sau đó trả về 60%

Bây giờ bạn có thể thực hiện một thay đổi nhỏ đối với lớp

Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
3

Bạn đã thêm một phương pháp

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
764 áp dụng chính sách bảng lương hiện tại cho chính sách mới và sau đó thay thế nó. Bây giờ bạn có thể sửa đổi chương trình để áp dụng chính sách cho đối tượng
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
151

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
4

Chương trình truy cập vào

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
766, được đặt tại chỉ mục
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
767, tạo đối tượng
# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
749 và áp dụng chính sách cho nhân viên. Khi
Name: James
Age: 16
Gender: Male
Class: 10th
Enter the marks of the respective subjects
Literature: 50
Math: 60
Biology: 55
Physics: 46


Name:  James
Age:  16
Gender:  Male
Study in:  10th
Total Marks:  211
147 được gọi, thay đổi được phản ánh. Bạn có thể chạy chương trình để đánh giá đầu ra

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
5

Số tiền séc cho nhân viên Kevin Bacon, là nhân viên bán hàng, hiện là $1080 thay vì $1800. Đó là bởi vì

# In hr.py

class HourlyEmployee(Employee):
    def __init__(self, id, name, hours_worked, hour_rate):
        super().__init__(id, name)
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll(self):
        return self.hours_worked * self.hour_rate
749 đã được áp dụng cho tiền lương

Như bạn có thể thấy, bạn có thể hỗ trợ các thay đổi chỉ bằng cách thêm một chính sách mới và sửa đổi một vài giao diện. Đây là loại linh hoạt mà thiết kế chính sách dựa trên thành phần mang lại cho bạn

Lựa chọn giữa Kế thừa và Thành phần trong Python

Python, với tư cách là một ngôn ngữ lập trình hướng đối tượng, hỗ trợ cả kế thừa và thành phần. Bạn đã thấy rằng kế thừa được sử dụng tốt nhất để mô hình hóa một mối quan hệ, trong khi các mô hình thành phần a có một mối quan hệ

Đôi khi, thật khó để biết mối quan hệ giữa hai lớp nên như thế nào, nhưng bạn có thể làm theo các hướng dẫn sau

  • Sử dụng tính kế thừa đối với thành phần trong Python để mô hình hóa mối quan hệ rõ ràng. Đầu tiên, chứng minh mối quan hệ giữa lớp dẫn xuất và cơ sở của nó. Sau đó, đảo ngược mối quan hệ và cố gắng biện minh cho nó. Nếu bạn có thể biện minh cho mối quan hệ theo cả hai hướng, thì bạn không nên sử dụng tính kế thừa giữa chúng

  • Sử dụng tính kế thừa trên thành phần trong Python để tận dụng cả giao diện và triển khai của lớp cơ sở

  • Sử dụng tính kế thừa đối với thành phần trong Python để cung cấp các tính năng mixin cho một số lớp không liên quan khi chỉ có một triển khai tính năng đó

  • Sử dụng thành phần thay vì thừa kế trong Python để mô hình hóa mối quan hệ thúc đẩy việc triển khai lớp thành phần

  • Sử dụng thành phần thay vì thừa kế trong Python để tạo các thành phần có thể được sử dụng lại bởi nhiều lớp trong các ứng dụng Python của bạn

  • Sử dụng thành phần thay vì thừa kế trong Python để triển khai các nhóm hành vi và chính sách có thể được áp dụng thay thế cho các lớp khác để tùy chỉnh hành vi của chúng

  • Sử dụng thành phần thay vì thừa kế trong Python để cho phép thay đổi hành vi trong thời gian chạy mà không ảnh hưởng đến các lớp hiện có

Sự kết luận

Bạn đã khám phá tính kế thừa và thành phần trong Python. Bạn đã học về loại mối quan hệ mà thừa kế và thành phần tạo ra. Bạn cũng đã trải qua một loạt bài tập để hiểu cách kế thừa và thành phần được triển khai trong Python

Trong bài viết này, bạn đã học cách

  • Sử dụng tính kế thừa để thể hiện một mối quan hệ giữa hai lớp
  • Đánh giá nếu thừa kế là mối quan hệ đúng
  • Sử dụng đa kế thừa trong Python và đánh giá MRO của Python để khắc phục sự cố đa kế thừa
  • Mở rộng các lớp với mixin và sử dụng lại việc triển khai chúng
  • Sử dụng thành phần để thể hiện một mối quan hệ giữa hai lớp
  • Cung cấp các thiết kế linh hoạt bằng cách sử dụng thành phần
  • Sử dụng lại mã hiện có thông qua thiết kế chính sách dựa trên thành phần

đề xuất đọc

Dưới đây là một số sách và bài viết khám phá sâu hơn về thiết kế hướng đối tượng và có thể hữu ích để giúp bạn hiểu cách sử dụng chính xác tính kế thừa và thành phần trong Python hoặc các ngôn ngữ khác

  • mẫu thiết kế. Các yếu tố của phần mềm hướng đối tượng tái sử dụng
  • Các mẫu thiết kế đầu tiên. Hướng dẫn thân thiện với não bộ
  • Mã sạch. Sổ tay thủ công phần mềm linh hoạt
  • Nguyên tắc RẮN
  • Nguyên tắc thay thế Liskov

Đánh dấu là đã hoàn thành

Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Kế thừa và thành phần. Hướng dẫn OOP Python

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Kế thừa đa cấp có được hỗ trợ trong python không?

Gửi cho tôi thủ thuật Python »

Giới thiệu về Isaac Rodríguez

Kế thừa đa cấp có được hỗ trợ trong python không?
Kế thừa đa cấp có được hỗ trợ trong python không?

Xin chào, tôi là Isaac. Tôi xây dựng, lãnh đạo và cố vấn cho các nhóm phát triển phần mềm và trong vài năm qua, tôi đã tập trung vào các dịch vụ đám mây và ứng dụng phụ trợ bằng Python cùng với các ngôn ngữ khác. Rất thích nghe từ bạn ở đây tại Real Python

» Thông tin thêm về Y-sác


Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Kế thừa đa cấp có được hỗ trợ trong python không?

Alex

Kế thừa đa cấp có được hỗ trợ trong python không?

Aldren

Kế thừa đa cấp có được hỗ trợ trong python không?

Joanna

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Kế thừa đa cấp có được hỗ trợ trong python không?

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Level Up Your Python Skills »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Level Up Your Python Skills »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Which inheritance is supported in Python?

Trả lời. Không giống như các ngôn ngữ lập trình hướng đối tượng khác như Java, Python hỗ trợ tất cả các kiểu kế thừa , thậm chí là đa kế thừa.

Kế thừa đa cấp trong Python có nghĩa là gì?

Trong python, Kế thừa đa cấp là một loại kế thừa được sử dụng để kế thừa cả các tính năng của lớp cơ sở và lớp dẫn xuất sang lớp dẫn xuất mới khi .