Thừa kế đơn trong ví dụ thời gian thực python

In this article, you’ll explore inheritance and composition in Python. Inheritance and composition are two important concepts in object oriented programming that model the relationship between two classes. They are the building blocks of object oriented design, and they help programmers to write reusable code

By the end of this article, you’ll know how to

  • Use inheritance in Python
  • Model class hierarchies using inheritance
  • Use multiple inheritance in Python and understand its drawbacks
  • Use composition to create complex objects
  • Reuse existing code by applying composition
  • Change application behavior at run-time through composition

Free Bonus. Click here to get access to a free Python OOP Cheat Sheet that points you to the best tutorials, videos, and books to learn more about Object-Oriented Programming with Python

What Are Inheritance and Composition?

Inheritance and composition are two major concepts in object oriented programming that model the relationship between two classes. They drive the design of an application and determine how the application should evolve as new features are added or requirements change

Both of them enable code reuse, but they do it in different ways

Remove ads

What’s Inheritance?

Inheritance models what is called an is a relationship. This means that when you have 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
6 class that inherits from 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 class, you created a relationship where
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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 is a specialized version of
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

Inheritance is represented using the Unified Modeling Language or UML in the following way

Classes are represented as boxes with the class name on top. The inheritance relationship is represented by an arrow from the derived class pointing to the base class. The word extends is usually added to the arrow

Note. In an inheritance relationship

  • Classes that inherit from another are called derived classes, subclasses, or subtypes
  • Classes from which other classes are derived are called base classes or super classes
  • A derived class is said to derive, inherit, or extend a base class

Let’s say you have a base class

# 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 and you derive from it to create 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 class. The inheritance relationship states that 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 is an
# 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. This means that
# 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 inherits the interface and implementation of
# 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, and
# 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 objects can be used to replace
# 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 objects in the application

This is known as the Liskov substitution principle. The principle states that “in a computer program, if

# 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 is a subtype of
# 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, then objects of type
# 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 may be replaced with objects of type
# 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 without altering any of the desired properties of the program”

You’ll see in this article why you should always follow the Liskov substitution principle when creating your class hierarchies, and the problems you’ll run into if you don’t

What’s Composition?

Composition is a concept that models a has a relationship. It enables creating complex types by combining objects of other types. This means that a class

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
602 can contain an object of another class
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
603. This relationship means that 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
602 has 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
603

UML represents composition as follows

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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
603 instances 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
602 class will contain

In the diagram above, 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
608 represents that 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
602 class contains one object of type
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
603. Cardinality can be expressed in the following ways

  • A number indicates the number of
    # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    603 instances that are contained in 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
    
    602
  • The * symbol indicates that 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
    
    602 class can contain a variable number of
    # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    603 instances
  • A range 1. 4 indicates that 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
    
    602 class can contain a range of
    # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    603 instances. The range is indicated with the minimum and maximum number of instances, or minimum and many instances like in 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
618. 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
618

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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
622 classes can leverage the functionality of
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
618 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

Remove ads

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

>>>

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
8

You declared a class

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
624 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
625 function to list its members

>>>

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
625 returns a list of all the members in the specified object. You have not declared any members 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
624, so where is the list coming from? You can find out using the interactive interpreter

>>>

# 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
4

As you can see, the two lists are nearly identical. There are some additional members 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
624 like
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
629 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
630, but every single member 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
631 class is also present 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
624

This is because every class you create in Python implicitly derives from

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
631. You could be more explicit and write
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
634, but it’s redundant and unnecessary

Ghi chú. In Python 2, you have to explicitly derive from

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
631 for reasons beyond the scope of this article, but you can read about it in the New-style and classic classes section of the Python 2 documentation

Exceptions Are an Exception

Every class that you create in Python will implicitly derive from

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
631. The exception to this rule are classes used to indicate errors by raising an exception

You can see the problem using the Python interactive interpreter

>>>

# 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
4

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 ngoại lệ đó thuộc loạ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
637 không phả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
638 và tấ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
639

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
640 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 lấy lớp của mì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
640 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ừ
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
642, từ đó 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
640

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

>>>

# 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
2

Như bạn có thể thấy, khi bạn nâng cấ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
638, đầu ra sẽ hiển thị chính xác loại lỗi được nâng cấp

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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
645 xử lý bảng lươ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
5

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
645 triển khai 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
647 lấy một tập hợp nhân viên và 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
648,
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
649 của họ và kiểm tra số lượng bằng 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
647 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ở

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 xử lý giao diện chung cho mọi loại nhân viên

# 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
2

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
648 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
649. Những gì bạn đang nói là mỗ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
651 phải được gán 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
648 cũng như một tên

Hệ thống nhân sự yêu cầu mỗ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
651 được xử lý phải cung cấp 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
647 trả về tiền lương hàng tuần cho nhân viên. Việc triển khai giao diện đó khác nhau tùy thuộc vào loạ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
651

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 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

Bạn tạo một lớp 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
660 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
651. Lớp được khởi tạo 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
648 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
649 theo yêu cầu của lớp cơ sở và bạ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
664 để 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ề
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
664 trong Supercharge Your Classes With Python super[]

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
660 cũng yêu cầu tham số 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
667 đạ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 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
647 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
667

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 một số 1670 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
670 được khởi tạo 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
648 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
649, giống như lớp cơ sở, cộ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
674 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
675 cần thiết để tính bảng lương. 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
647 method is implemented by returning the hours worked times the hour rate

Finally, the company employs sales associates that are paid through a fixed salary plus a commission based on their sales, so you create 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
677 class

# 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

You derive

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
677 from
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
660 because both classes have 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
667 to consider. At the same time,
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
677 is initialized with 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
682 value that is based on the sales for the employee

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
647 leverages the implementation of the base class to retrieve 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
684 salary and adds the commission value

Since

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
677 derives from
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
660, you have access 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
667 property directly, and you could’ve 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
647 using the value of that property

The problem with accessing the property directly is that if the implementation of

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
689 changes, then you’ll have to also change the implementation of
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
690. It’s better to rely on the already implemented method in the base class and extend the functionality as needed

You created your first class hierarchy for the system. Sơ đồ UML của các lớp trông như thế này

The diagram shows the inheritance hierarchy of the classes. The derived classes implement 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
691 interface, which is required by 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
645. 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
693 implementation requires that 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
694 objects passed contain 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
648,
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
649, 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
697 implementation

Interfaces are represented similarly to classes with the word interface above the interface name. Interface names are usually prefixed with a capital

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
698

The application creates its employees and passes them to the payroll system to process payroll

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

You can run the program in the command line and see the results

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

The program creates three employee objects, one for each of the derived classes. Then, it creates the payroll system and passes a list of the employees to its

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
647 method, which calculates the payroll for each employee and prints the results

Notice how 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
651 base class doesn’t define 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
647 method. This means that if you were to create a plain
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 object and pass it 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
645, then you’d get an error. You can try it in the Python 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
62

While you can instantiate 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
651 object, the object can’t be used by 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
645. Why? Because it can’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
647 for 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
651. To meet the requirements of
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
645, you’ll want to convert 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
651 class, which is currently a concrete class, to an abstract class. That way, no employee is ever just 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
651, but one that implements
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
647

Remove ads

Abstract Base Classes in Python

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
651 class in the example above is what is called an abstract base class. Abstract base classes exist to be inherited, but never instantiated. Python provides the
# 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
413 module to define abstract base classes

You can use leading underscores in your class name to communicate that objects of that class should not be created. 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

# 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
413 module in the Python standard library provides functionality to prevent creating objects from abstract base classes

You can modify 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
651 class to ensure that it can’t be instantiated

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

You derive

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 from
# 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
417, making it an abstract base class. Then, you decorate 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
647 method with the
# 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
419 decorator

This change has two nice side-effects

  1. You’re telling users of the module that objects of type
    # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    651 can’t be created
  2. You’re telling other developers working on the
    # 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
    
    421 module that if they derive from
    # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    651, then they must override 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
    
    647 abstract method

You can see that objects of type

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 can’t be created using 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
64

The output shows that the class cannot be instantiated because it contains an abstract 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
697. Derived classes must override the method to allow creating objects of their type

Implementation Inheritance vs Interface Inheritance

When you derive one class from another, the derived class inherits both

  1. The base class interface. The derived class inherits all the methods, properties, and attributes of the base class

  2. The base class implementation. The derived class inherits the code that implements the class interface

Most of the time, you’ll want to inherit the implementation of a class, but you will want to implement multiple interfaces, so your objects can be used in different situations

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. They allow you to inherit from a single class, but you can implement multiple interfaces

In Python, you don’t have to explicitly declare an interface. Any object that implements the desired interface can be used in place of another object. This is known as duck typing. 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

# 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
426 vào ví dụ trên mà không 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
651

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

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
426 không xuất phát 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
651, nhưng nó hiển thị cùng một giao diện được yêu cầu bở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
645.
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
693 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
    # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    648 trả về id của nhân viên
  • Thuộc tính hoặc 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
    
    649 đại diện cho tên của nhân viên
  • 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
    
    647 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 đáp ứng bởi 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
426, vì vậ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
645 vẫn có thể tính toán biên chế của mình

Bạn có thể sửa đổi chương trình để sử dụng 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
426

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

Chương trình tạo một đố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
426 và thêm nó vào danh sách được xử lý bở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
645. Bây giờ bạn có thể chạy chương trình và 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
67

Như bạn 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
645 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

    # 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
    
    441 cũng có thể 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
    
    648 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
    
    649, như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
    
    441 không phải 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
    
    651, 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 được yêu cầu 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

# 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
446 rồi sửa đổi mô-đun
# 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
421 về trạng thái ban đầ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
68

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

# 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
413 vì 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
651 không cần phải trừu tượng. Bạn cũng đã xóa phương thức trừu 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
697 khỏi phương thức này 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
648 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
649 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
651 trong các lớp dẫn xuất của bạn. 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
647 chỉ là một giao diện của 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
693, nên bạn không cần triển khai nó trong lớp cơ 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
651

Lưu ý cách 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
677 bắt nguồn 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
660. Điều này 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
677 kế thừa triển khai và giao diện 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
660. Bạn có thể thấy cách 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
690 tận dụng việc triển khai lớp cơ sở vì nó dựa vào kết quả 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
462 để triển khai phiên bản của chính nó

Remove ads

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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 được sử dụng bở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
645 để 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
# 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
465 mới

# 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
465 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ý. Họ làm tất cả các công việc giấy tờ cho người quản lý và đảm bảo rằng mọi thứ được lập hóa đơn và thanh toán đúng hạn. 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 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
# 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
421 bởi vì bây giờ chúng cũng được sử dụng bởi
# 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
465

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

# 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
470 và di chuyển các lớp đế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
69

Việc triển khai vẫn giữ nguyên, nhưng bạn chuyển các lớp sang 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
694. Bây giờ, bạn thay đổi chương trình của mình để hỗ trợ thay đổi

# 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
40

Bạn chạy chương trình và xác minh rằng nó vẫ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
61

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

# 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
42

Đầu tiên, bạn thêm 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
472 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
660. Lớp hiển thị một phương thức
# 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
474 sẽ được sử dụng bởi hệ thống năng suất. Phương pháp lấy
# 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
475 nhân viên đã làm việc

Sau đó, bạn thêm

# 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
476,
# 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
477 và
# 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
478 rồi triển khai giao diện
# 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
474 để 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

# 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
480

# 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
43

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

# 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
481 lấy danh sách nhân viên và số giờ cần 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

# 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
44

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

# 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
45

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

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

Remove ads

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

# 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
482 thực hiện vai trò của 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
476 trong ngữ cảnh của 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
465, nhưng đối với mục đích tính lương, nó là 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
670

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ừ

    # 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
    
    476. Bạn có thể lấy 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
    
    476 để kế thừa phương thức
    # 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
    
    488 cho vai trò, sau đó ghi đè 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
    
    647 để triển khai nó dưới 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
    
    670

  2. Xuất phát 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
    
    670. Bạn có thể 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
    
    670 để kế thừa 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
    
    647, sau đó ghi đè phương thức
    # 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
    
    488 để triển khai nó dưới dạ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
    
    476

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ả

# 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
476 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
670

# 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
46

Python cho phép bạn kế thừa từ hai lớp khác nhau bằng cách chỉ định chúng giữa dấu ngoặc đơn trong khai báo lớp

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

# 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
47

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

# 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
48

Bạn nhận được 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
637 nói rằ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
499 đối số vị trí được mong đợi, như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
400 đã được đưa ra

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

# 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
482 đầu tiên 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
476 và sau đó 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
670, vì vậy trình thông dịch đang cố gắng sử dụ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
404 để khởi tạo đối tượng

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

# 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
49

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 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
40

Bây giờ có vẻ như bạn đang thiếu một 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
667, tham số này cần thiết để khởi tạo
# 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
476, nhưng tham số đó không có ý nghĩa trong ngữ cảnh của 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
482 vì nó 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
670

Có thể triển khai

# 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
409 sẽ giúp ích

# 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
41

Thử nó

# 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
42

Đ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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
664 để 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ề
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
664 trong Supercharge Your Classs With Python super[]

Bạn có thể đánh giá MRO 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
482 bằng trình thông dịch tương tác

>>>

# 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
43

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

# 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
482

  1. Phương phá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
    
    414 được gọi là

  2. Cuộc gọi ________ 5415 khớp với ________ 5416

  3. # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    670 gọi
    # 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
    
    418, mà MRO sẽ khớp với
    # 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
    
    404, được kế thừa 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
    
    420

Bởi vì các tham số không khớp, 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
637 đượ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 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
422 như sau

# 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
44

Đ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 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
45

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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
647 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
424 trước phương thứ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
670. Bạn cần 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
647 trong
# 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
482 và gọi triển khai đúng 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
46

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
697 gọi trực tiế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
429 để đả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 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
47

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

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

# 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
482 sử dụng đa kế thừa để xuất phát từ hai lớp mà cuối cùng cũng xuất phát 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
651. Điều này khiến hai đường dẫn đến lớp cơ 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
651, đâ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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 đượ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 thực hiện các thay đổi đối với mô-đun năng suấ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
48

Mô-đun

# 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
434 triển khai 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
465, cũng như các vai trò liên quan mà nó hỗ trợ. Các lớp triển khai giao diện
# 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
474 theo yêu cầu của hệ thống, nhưng chúng không 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
651

Bạn có thể làm tương tự với mô-đun

# 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
421

# 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
49

Mô-đun

# 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
421 thực hiệ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
645, tính toán bảng lương cho nhân viên. Nó cũng thực hiện các lớp chính sách cho bảng lương. Như bạn có thể thấy, các lớp chính sách không 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
651 nữa

Bây giờ bạn có thể thêm các lớp cần thiết vào 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
694

# 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
20

Mô-đun

# 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
470 nhập các chính sách và vai trò từ các mô-đun khác và triển khai các loạ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
651 khác nhau. Bạn vẫn đang sử dụng đa kế thừa để kế thừa việc triển khai các lớp chính sách tiền lương và vai trò năng suất, nhưng việc triển khai từng lớp chỉ cần xử lý việc khởi tạo

Lưu ý rằng bạn vẫn cần khởi tạo rõ ràng các chính sách tiền lương trong hàm tạo. Bạn có thể thấy rằng phần khởi tạo 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
472 và
# 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
476 giống hệt nhau. Ngoài ra, khởi tạo 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
478 và
# 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
482 giống nhau

Bạn sẽ không muốn có kiểu sao chép mã này trong các thiết kế phức tạp hơn, vì vậy bạn phải cẩn thận khi thiết kế hệ thống phân cấp lớp

Đây là sơ đồ UML cho thiết kế mới

Sơ đồ hiển thị các mối quan hệ để xác định

# 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
476 và
# 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
482 bằng cách sử dụng đa thừa kế, nhưng tránh được vấn đề kim cương

Bạn có thể chạy chương trình và xem nó hoạt động như thế nào

# 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
21

Bạn đã thấy cách hoạt động của thừa kế và đa thừa kế trong Python. Bây giờ bạn có thể khám phá chủ đề sáng tác

Remove ads

Thành phần trong Python

Thành phần là một khái niệm thiết kế hướng đối tượng mà mô hình có một mối quan hệ. Trong thành phần, một lớp được gọi là hỗn hợp chứa một đối tượng của một lớp khác được gọi là thành phần. Nói cách khác, một lớp tổng hợp có một thành phần của lớp khác

Thành phần cho phép các lớp tổng hợp sử dụng lại việc triển khai các thành phần mà nó chứa. Lớp tổng hợp không kế thừa giao diện lớp thành phần, nhưng nó có thể tận dụng việc triển khai của nó

Mối quan hệ thành phần giữa hai lớp được coi là liên kết lỏng lẻo. Điều đó có nghĩa là những thay đổi đối với lớp thành phần hiếm khi ảnh hưởng đến lớp tổng hợp và những thay đổi đối với lớp tổng hợp không bao giờ ảnh hưởng đến lớp thành phần

Điều này cung cấp khả năng thích ứng tốt hơn để thay đổi và cho phép các ứng dụng đưa ra các yêu cầu mới mà không ảnh hưởng đến mã hiện có

Khi xem xét hai thiết kế phần mềm cạnh tranh, một dựa trên tính kế thừa và một dựa trên thành phần, giải pháp thành phần thường là linh hoạt nhất. Bây giờ bạn có thể xem cách sáng tác hoạt động

Bạn đã sử dụng bố cục trong các ví dụ của chúng tôi. Nếu bạn nhìn vào 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
651, bạn sẽ thấy rằng nó chứa hai thuộc tính

  1. # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    648 để xác định một nhân viên
  2. # In hr.py
    
    class HourlyEmployee[Employee]:
        def __init__[self, id, name, hours_worked, hour_rate]:
            super[].__init__[id, name]
            self.hours_worked = hours_worked
            self.hour_rate = hour_rate
    
        def calculate_payroll[self]:
            return self.hours_worked * self.hour_rate
    
    649 để chứa tên của nhân viên

Hai thuộc tính này là các đối tượng 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
651 có. Do đó, bạn có thể nói rằ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
651 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
648 và có một tên

Một thuộc tính khác cho 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
651 có thể 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
458

# 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
22

Bạn đã triển khai một lớp địa chỉ cơ bản chứa các thành phần thông thường cho một địa chỉ. Bạn đã đặt thuộc tính

# 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
459 là tùy chọn vì không phải địa chỉ nào cũng có thành phần đó

Bạn đã triển khai

# 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
460 để cung cấp một đại diện đẹp mắt của 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
458. Bạn có thể thấy cách triển khai này trong trình thông dịch tương tác

>>>

# 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
23

Khi bạn

# 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
462 biến
# 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
463, phương thức đặc biệ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
460 được gọi. 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. Quá tải toán tử và chức năng trong các lớp Python tùy chỉnh cung cấp một cái nhìn tổng quan về các phương thức đặc biệt có sẵn trong các lớp có thể được triển khai để tùy chỉnh hành vi của các đối tượng của bạn

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

# 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
458 vào 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
651 thông qua thành phần

# 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
24

Bây giờ, bạn khởi tạo thuộc tính

# 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
463 thành
# 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
468 để biến nó thành tùy chọn, nhưng bằng cách đó, giờ đây bạn có thể gán 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
458 cho 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
651. Cũng lưu ý rằng không có tham chiếu nào trong 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
694 đến mô-đun
# 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
472

Thành phần là một mối quan hệ kết hợp lỏng lẻo thường không yêu cầu lớp tổng hợp phải có kiến ​​thức về thành phần

Sơ đồ UML thể hiện mối quan hệ giữ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
651 và
# 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
458 trông như thế này

Sơ đồ cho thấy mối quan hệ thành phần cơ bản giữ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
651 và
# 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
458

Bây giờ bạn có thể sửa đổ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
645 để tận dụng thuộc tính
# 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
463 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
651

# 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
25

Bạn kiểm tra xem đố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
694 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 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
26

Bạn đã thêm một vài địa chỉ vào 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
481 và
# 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
482. Khi bạn chạy chương trình, bạn sẽ thấy các địa chỉ được in

# 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
27

Lưu ý cách đầu ra bảng lương cho 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
481 và
# 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
482 hiển thị địa chỉ nơi séc được gử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
651 tận dụng việc triển khai 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
458 mà không cần biết đố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
458 là gì hoặc nó được biểu diễn như thế nào. 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 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
458 mà không ảnh hưởng đến 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
651

Remove ads

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

Thành phần linh hoạt hơn thừa kế vì nó mô hình hóa mối quan hệ kết hợp lỏng lẻo. 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 các yêu cầu 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
645 và
# 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
465

Bạn có thể bắt đầu bằng cách triển khai chức năng 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
465

# 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
28

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
465 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 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
494, đượ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 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
495 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 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
496, 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 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
29

Mỗi vai trò bạn đã triển khai hiển thị 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
497 lấy số lượ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
475 đã 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
645 cho ứng dụ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
50

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
645 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 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
201, được cung cấp cho một 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
648, trả về chính sách trả lương của nó. Nếu 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
648 đượ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 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
495

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
647 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 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
51

Trước tiên, bạn triển khai 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
206 đó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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
674, 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 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
206. 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 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
206. Ngoài ra,
# 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
210,
# 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
211 và
# 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
212 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
206

# 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
210 được khởi tạo 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
667 sau đó được sử dụng 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
647.
# 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
211 được khởi tạo 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
675 và 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
647 bằng cách tận dụng lớp cơ 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
674

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
212 bắt nguồn 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
210 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ố
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
667, nhưng nó cũng yêu cầu tham 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
224

# 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
224 được sử dụng để tính toán
# 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
226, được triển khai dưới dạng 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 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
226 là số lần bán hàng nhân với giá trị
# 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
224

# 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
212 triển khai 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
647 bằng cách trước tiên tận dụng việc triển khai trong
# 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
210 và sau đó thêm hoa hồng đã tính

Bây giờ bạn có thể thêm 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
232 để quản lý địa chỉ nhân viên

# 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
52

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
232 giữ một cơ sở dữ liệu nội bộ gồm 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
458 cho mỗi nhân viên. Nó hiển thị một phương thức
# 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
235 trả về địa chỉ của 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
648. Nếu 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
648 không tồn tại, thì nó sẽ 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
495

Việc triển khai 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
458 vẫn giống như trước đây

# 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
22

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

# 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
470 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 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
241

# 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
54

# 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
241 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
648,
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
649 và
# 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
245. Nó có một phiên bản 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
465,
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
645 và
# 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
232. 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 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
249 trả về danh sách nhân viên. 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
651 được tạo trong một phương thức nội bộ
# 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
251. Lưu ý rằng bạn không có các loại lớp họ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
651 khác nhau. Bạn chỉ cầ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
651 duy nhấ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
55

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
651 được khởi tạo với các 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
648,
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
649 và
# 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
463. Nó cũng yêu cầu năng suấ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
245 cho nhân viên và chính sách
# 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
259

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

# 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
488 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 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
261 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
245. Nói cách khác, nó ủy quyền cho đố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
245 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 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
259 để theo dõi công việc
# 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
475.
# 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
259, 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

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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 duy nhất bao gồm các đối tượng dữ liệu khác như
# 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
458 và phụ thuộc vào giao diện
# 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
269 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
691 để ủ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 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
56

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

# 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
57

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

Remove ads

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 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
481 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 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
58

Chương trình lấy danh sách nhân viên 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
241 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 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
211 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
645 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 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
59

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

Nguyên tắc thay thế Liskov là hướng dẫn quan trọng nhất để xác định xem kế thừa có phải là giải pháp thiết kế phù hợp hay không. 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 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
278 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 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
279. Suy nghĩ ban đầu của bạn là bạn có thể lấy
# 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
279 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
278 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 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
    
    279 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
    
    278. 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 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
    
    278 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
    
    279. Đả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 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
286 hiển thị một thuộc tính
# 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
287. Bạn cần 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
288, cũng có 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
287. Có vẻ như
# 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
288 là một loại đặc biệt 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
286, 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 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
288 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
286 vì diện tích của nó được tính từ tích 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
294 nhân với
# 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
295 của nó. Ràng buộc 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
296 và
# 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
297 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 ________ 6288 lại là ________ 6286. Hãy đảo ngược mối quan hệ để xem nó có hợp lý không

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
286 is 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
288 because its area is calculated from the product of its
# 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
294 times its
# 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
295. Sự khác biệt 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
504 và
# 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
505 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

You might have seen other examples that derive

# 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
288 from
# 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
286 to explain inheritance. You might be skeptical with the little test you just did. Đủ công bằng. Let’s write a program that illustrates the problem with deriving
# 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
288 from
# 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
286

First, you implement

# 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
286. You’re even going to encapsulate the attributes to ensure that all the constraints are met

# 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
20

The

# 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
286 class is initialized with 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
295 and 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
294, and it provides an
# 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
287 property that returns the area. The
# 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
295 and
# 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
294 are encapsulated to avoid changing them directly

Now, you derive

# 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
288 from
# 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
286 and override the necessary interface to meet the constraints of 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
288

# 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
21

The

# 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
288 class is initialized with 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
521, which is used to initialize both components of the base class. Now, you write a small program to test the behavior

# 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
22

The program creates 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
286 and 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
288 and asserts that their
# 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
287 is calculated correctly. You can run the program and see that everything is
# 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
525 so far

# 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
23

The program executes correctly, so it seems that

# 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
288 is just a special case of 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
286

Later on, you need to support resizing

# 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
286 objects, so you make the appropriate changes to the class

# 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
24

# 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
529 takes the
# 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
530 and
# 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
531 for the object. You can add the following code to the program to verify that it works correctly

# 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
25

You resize the rectangle object and assert that the new area is correct. You can run the program to verify the behavior

# 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
23

The assertion passes, and you see that the program runs correctly

So, what happens if you resize a square? Modify the program, and try to modify the

# 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
532 object

# 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
27

You pass the same parameters to

# 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
533 that you used with
# 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
534, and print the area. Khi bạn chạy chương trình, bạn sẽ thấy

# 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
28

The program shows that the new area is

# 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
535 like the
# 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
534 object. The problem now is that the
# 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
532 object no longer meets the
# 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
288 class constraint that the
# 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
295 and
# 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
294 must be equal

How can you fix that problem? You can try several approaches, but all of them will be awkward. You can override

# 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
529 in
# 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
532 and ignore the
# 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
294 parameter, but that will be confusing for people looking at other parts of the program where
# 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
544 are being resized and some of them are not getting the expected areas because they are really
# 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
545

In a small program like this one, it might be easy to spot the causes of the weird behavior, but in a more complex program, the problem will be harder to find

The reality is that if you’re able to justify an inheritance relationship between two classes both ways, you should not derive one class from another

In the example, it doesn’t make sense that

# 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
288 inherits the interface and implementation of
# 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
529 from
# 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
286. That doesn’t mean that
# 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
288 objects can’t be resized. Có nghĩa là giao diện khác vì nó chỉ cần tham 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
521

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 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
288 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
286 như thử nghiệm ở trên đã khuyên

Remove ads

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 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
553 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 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
553 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 từ ví dụ về thành phần

# 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
29

Sự thay đổi là rất nhỏ. Bạn vừa thay đổi thuộc tính

# 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
245 và
# 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
259 thà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 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
558

# 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
10

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
558 hiển thị phương thức
# 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
553 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 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
561 có nội dung: “Tạo ánh xạ từ điển
# 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
562 đến
# 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
563 cho mỗi mục trong
# 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
564 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
562 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651, bởi 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ừ

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
631 và một trong những thành viên đó 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
629, 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
629 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 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
570

# 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
571 kiểm tra giá trị đã chỉ định. Nếu giá trị 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
631, thì nó sẽ xem liệu nó cũng có thành viên
# 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
553 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à
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
631, thì nó chỉ trả về giá trị

Bạn có thể sửa đổ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
651 để hỗ trợ mixin này

# 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
11

Tất cả những gì bạn phải làm là kế thừ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
558 để 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 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
458, vì vậy thuộc tính
# 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
578 được biểu diễn theo cùng một cách

# 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
12

Bạn áp dụng mixin cho 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
458 để 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 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
13

Chương trình triển khai 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
580 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 biểu diễn từ điển được cung cấp bởi

# 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
553. Bạn có thể chạy chương trình để xem đầu ra của nó

# 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
14

Bạn đã tận dụng việc triển khai

# 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
558 trong cả hai 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
651 và
# 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
458 ngay cả khi chúng không liên quan. Bởi vì
# 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
558 chỉ cung cấp 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 đề

Remove ads

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ệ. Với thành phầ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
602 có một thể hiện 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
603 và có thể tận dụng việc triển khai nó. 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
603 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
602

Trong ví dụ thành phần ở trên, 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
651 có một đố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
458.
# 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
458 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ư

# 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
441 hoặc
# 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
594 có thể sử dụng lại
# 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
458 mà không liên quan đế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
651. 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 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
241, bạn sẽ nhận thấy rằng nó sử dụ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
251 để xây dựng 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
651 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 chỉ bằng cách chỉ định 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
648, ví dụ như
# 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
202

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 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
434

# 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
15

Trước tiên, bạn tạo nội bộ 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
204, sau đó cung cấp biến nội bộ
# 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
205 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 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
204. Thay vào đó, bạn cung cấp hai chức nă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
207 và
# 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
481, 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 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
204 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

# 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
421

# 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
16

Một lần nữa, bạn tạo nội bộ

# 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
211 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 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
472

# 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
17

Về cơ bản, bạn đang nói rằng chỉ nên có 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
213, 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
211 và 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
204. 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

# 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
470. Bạn cũng sẽ tạo một Singleton trong 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
217, nhưng bạn sẽ thực hiện một số thay đổi bổ sung

# 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
18

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 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
217 đượ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

You changed the

# 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
219 attribute to be a dictionary where the key is the employee
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
648 and the value is the employee information. Bạn cũng đã sử dụng một phương thức
# 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
221 để trả về thông tin cho nhân viên được chỉ định
# 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
222

Thuộc tính

# 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
223 hiện sắp xếp các khóa để trả lại cho nhân viên được sắp xếp theo
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
648 của họ. Bạn đã thay thế phương thức đã xây dựng 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
651 bằng các lệnh gọi trực tiếp đến bộ 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
651

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
651 hiện được khởi tạo 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
648 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 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
19

Bạn nhập các hàm có liên quan từ các mô-đun

# 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
421 và
# 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
434, cũng như 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
231 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
651. 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 trực tiếp chỉ bằng cách sử dụng đố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
648 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 đây, nhưng bây giờ bạn có thể thấy rằng 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
651 duy nhất có thể được tạo 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
648 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 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 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
458 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 cũng chứa vai trò năng suất do mô-đun
# 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
434 cung cấp và chính sách trả lương do mô-đun
# 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
421 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 để theo dõi công việc theo phương thức
# 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
488 và để tính bảng lương theo 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
647

Bạn đang sử dụng bố cục theo hai cách khác nhau. 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
458 cung cấp dữ liệu bổ sung 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
651 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651 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. Ghi đè phương thức là cách duy nhất để tùy chỉnh hành vi của lớp cơ sở. Đ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 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
249 không kế thừ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
206, 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 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
206

# 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
249 khởi tạo
# 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
253 thành
# 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
468 và cung cấp một phương thức
# 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
255 nội bộ sẽ đưa ra một ngoại lệ 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
256 chưa được áp dụng. Sau đó, nó cung cấp một phương thức
# 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
257 để gán
# 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
253

Trước tiên, giao diện công khai kiểm tra xem

# 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
253 đã được áp dụng chưa, sau đó triển khai chức năng theo chính sách cơ sở đó. Phương phá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
260 chỉ ủy quyền cho chính sách cơ sở 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
647 sử dụng nó để tính toán
# 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
262 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

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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 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
264 áp dụng chính sách bảng lương hiện có 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
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
651

# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__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 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
266, được đặt tại chỉ mục
# 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
267, tạo đố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
249 và áp dụng chính sách cho nhân viên. Khi
# In hr.py

class HourlyEmployee[Employee]:
    def __init__[self, id, name, hours_worked, hour_rate]:
        super[].__init__[id, name]
        self.hours_worked = hours_worked
        self.hour_rate = hour_rate

    def calculate_payroll[self]:
        return self.hours_worked * self.hour_rate
647 đượ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à vì

# 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
249 đã đượ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

Gửi cho tôi thủ thuật Python »

Giới thiệu về Isaac Rodríguez

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

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Alex

Aldren

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

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

Nâng cao kỹ năng Python của bạn »

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

Nâng cao kỹ năng Python của bạn »

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

Kế thừa đơn lẻ với ví dụ trong python là gì?

Kế thừa đơn trong Python . Để tóm tắt ví dụ này. Có một lớp con gọi là Cat kế thừa các thuộc tính và phương thức của lớp cơ sở Pet. Ngoài ra, lớp Cat còn có phương thức riêng, sound. a child class inherits from a single parent class as shown below. To summarize this example: There is a subclass called Cat that inherits the attributes and methods of the base class Pet. In addition, the class Cat also has its own method, sounds.

Các ví dụ thời gian thực về thừa kế đơn lẻ là gì?

Ví dụ, chúng ta là con người. Chúng tôi thừa hưởng một số thuộc tính từ lớp 'Con người' như khả năng nói, thở, ăn, uống, v.v. Chúng ta cũng có thể lấy ví dụ về ô tô. Lớp 'Xe hơi' kế thừa các thuộc tính của nó từ lớp 'Ô tô', lớp này thừa hưởng một số thuộc tính của nó từ một lớp 'Xe cộ' khác

Kế thừa trong python với ví dụ thời gian thực là gì?

Tính kế thừa trong Python giúp nhà phát triển sử dụng lại các đối tượng . Mỗi khi một lớp kế thừa lớp cơ sở, nó sẽ có quyền truy cập vào chức năng của đối tượng cha. Khả năng sử dụng lại do kế thừa cũng đáng tin cậy vì lớp cơ sở đã được kiểm tra. Việc chuẩn hóa giao diện giữa các lớp trở nên dễ dàng.

Kế thừa cấp đơn giải thích với ví dụ lập trình là gì?

Kế thừa đơn trong C++ . Nó là loại đơn giản nhất trong số tất cả các loại thừa kế vì nó không bao gồm bất kỳ loại kết hợp thừa kế nào hoặc các mức độ thừa kế khác nhau. The inheritance in which a single derived class is inherited from a single base class is known as the Single Inheritance. It is the simplest among all the types of inheritance since it does not include any kind of inheritance combination or different levels of inheritance.

Chủ Đề