Heli Mu Yi ---- Package

zhaozj2021-02-17  41

Mustard

---- Package

Author: HolyFire

Talking about the object-oriented OOP, someone shouted in the package.

What is the package? Why is it to package?

Yes, there is no packaging, the package is due to people's needs, just as the computer comes to the human, the programming language enters your brain, naturally.

When there is no package, people are in programming, and they do not have the appearance of the package to become a person, so the package is not so mysterious, terrible.

Imagine, there are many packages in our lives: food is placed in a bag, used to prevent mixing dust and insect erosion; wear sunglasses, prevent ultraviolet injury, lying in the safe, preventing unfair people .

Ok, we see a little, a role in the package is to protect our things.

In life, there are many practices. Here is to mention it, it is true, this is a real style, of course, this is an ideal situation, but people often like to create This kind of thing is like "flowing low flow". Newton said "Due to the gravity of the earth, Apple is on the ground." Fortunately, there is such a truth, we have water, there is apple. Of course, it is difficult to find an antique on the earth, so we don't need to consider the possibility of flying on the sky. Another example is 20Hz ~~ 20khz. If someone says there will be a person's hearing exceeds this range, I still have a point of treatment, but I still treat people as 20Hz ~~ 20kHz. On the disclast, the little probability will not happen, I don't plan to confirm him, I just want to use him, I only deal with the hearing range of 20Hz ~~ 20kHz, in most areas, this is enough, Let people who have special requirements in this regard to struggle, leverage these individual people will bring more benefits.

We can also find that the benefits of the package can reduce complexity.

I think no one will be stupid to add a sheep and a cattle to the results of two sheep or two cattle, but the result of two animals is acceptable.

In this way, it is possible to encapsulate him to reduce the possibility of an error.

The package is far away is far away.

Wait, you are full of doubts in your heart.

After this long, why not explain what is packaged, and there is not mentioned in the text.

Smart friends, is not something, nothing, he is a thinking of people who consider and solve problems, he is your thoughts, you are your inspiration, it is your kit. Package is the program that people can make more convenient and safer, and the outstanding outstanding people who are more convenient to do in order to better manage and use things. .

In the programming application, let's take a look at the benefits of packaging us.

Due to the computer, some are digital, the information is stored in many memory cells, and these storage units have indeed sizes due to hardware restrictions.

First let's talk about protecting our data from casual, this is what I mentioned.

For example, an employee can see his salary, but if he can modify, I am afraid that everyone wants to go to this company. Modify the salary data, don't work, don't work, but if everyone can modify the salary, it will not work, the financial will need to modify data about salary.

Here I use the C language designed for object-oriented development because he is an object-oriented design, so it is more convenient to achieve. People who haven't studied C first depends on the basic concepts, at least understand the concept of private, public, protected, friends and other keywords. Let's understand what we have to do. There are many people in the company. Everyone has salary, some are employees, employees to use the work number to distinguish, and some are financial accounts, and the accounting is also employees, and employees can look at salary, and financial accounts can also Modify salary.

We have three characters, people, employees, accounting, do two things, employees to see wages, payments to revise salary, then clarify their relationship

The three names, ages, gender are what we need to deal with. We must do it to deal with salary, some unrelated information can be omitted, and these three information that needs people in each department.

人 = {Name, age, gender}

Employees are people, accounting is people, accounting is employee, employee has salary, in order to distinguish employee every employee

Employees look at wages, revisit payables

Employees = {People, Wages, Tier Numerals, Warate}

Finance = {employee, modify salary}

// The following three sentences are the standard function library, which can save us a lot of efforts, so that we understand what we have to understand, skip the branching section

#include

#include

Using namespace std;

Class Man {

PUBLIC:

Enum sextype {mele, female}; // enumeration type, gender only two kinds of men and women, here don't consider the demon, if the input is not the type, the compiler will alarm. If the type conversion is not the two values, it will also be alarm so we reduce the error caused by mistake input.

PRIVATE: // Variables cannot be operated, set to private, only class member functions can operate,

String name; // Name, String is the class provided in the standard library, which can be easily handled

Unsigned age; // age, because the age is not a negative number, so use unsigned, this can prevent logic errors

Sextype sex; // gender

Public: // Outside the interface, of course it is open

String getName (void) {return name;} // Get the name of the person

Void setName // change the name of the person, the name is legal here

{

IF (str == "")

Return;

Name = name;

}

Unsigned getage (void) {return age;} // get people's age

Void setage // Changing people's age, because employees retired 50 years old, so 50 or less

{

IF (agn> = 50)

Return;

Age = age;

}

Sextype getsex (void) {return sex;} // Get people's gender

Void setsex (sextype sex) {sex = sex} // change the gender};

Class Accountant;

Class Employee: Public Man {

Private:

UNSIGNED ID; //

Protected: // Although it is protected, his successor will be operated

Unsigned pay; // salary

PUBLIC:

Unsigned getId (void); // get the work number

Void setID (unsigned); // change the work number

Unsigned getpay (void) {return pay;} // see wage

Friend class accountant; // Since the financial will modify the salary of all employees, the access rights trust will be given a financial meeting.

}

Class Accountant: Public Employee {

PUBLIC:

Void setPay (unsigned pay) {pay = pay;} // change your salary

Void setPay (Employee * man, unsigned pay) {man-> pay = pay;}; // change someone else's salary

}

Of course, this is a well-designed package, simplifies the structure. It is because of its heart-hearting analysis, it makes it easy to design easily. It is due to the correct, well, and the package is a thought. We now reflect him. come out.

Look at this example, the type of salary is unsigned, non-negative integer, huh, huh, everyone is not willing to work for the payment of salary, the simplified problem is from the perspective of people, if your boss considers salary For negative situation, then ... ^ _ ^

Now a careful fisher will not make mistakes. He knocked a 0, wow, please guest, but this fiscal can be miserable, this kind of good thing will not happen, not allowed to happen, boss green face .

The design has to add a salary ceiling, no way, is there a state in the state?

Class Accountant: Public Employee {

Enum maxpay {maxpay = 8000};

PUBLIC:

Void setPay (unsigned pay)

{

IF (Pay> MAXPAY)

Return;

Pay = pay;

}

Void setPay (Employee * Man, unsigned pay)

{

IF (Pay> MAXPAY)

Return;

MAN-> pay = pay;

}

}

It can be seen that the role of the package is to reduce the possible possible, convenient and flexible use type.

In the above example we see that class class is composed of some variables and functions, these variables and functions are part of the class, we call it a member, the variable is a member variable, and the function is of course called the member function. Why do we think that things are composed of substances and motions, and the performance of the material is usually described with some of his attributes, what he has, showing a movement. We usually use a process, to put a thing information The description is clear that these two things are needed. In long-term practice, the programmer reached a consensus, the characteristics of the things (that is, it have it) are called attributes. He can generate the behavior of the behavior, and the digitization is a member variable and member function, and their composition Overall is class (type), this type will be considered as a separate node, just like MAN in the example, we will not say that this is a master of name, age, etc., but as a class - Class MAN considers to simplify problems. Small things have become bigger, large things are combined into greater things, so that the big problem can also be treated with small modules, which is the place of encapsulation and his mission. What needs to be added is that since we protect the data, how to let users access these data is a problem. In the above example, it is seen that the public: declared method, the user can be used, and we are through these methods Tell the user to the user, here we will describe the part of the method is called the interface (in C , the statement of the member function of the class, the user is generally interested in the interface of the public: section, so some people recommend put public: section The content is written in a conspicuous place, such as the top of the class), and some people say that the interface is the channel, which is a channel for communication and communication, so the interface is very important. He directly related to your class. .

When the user uses an interface of the class, it is like a notification this type to use a behavior, just like it is to pass a message, we call us the interface called to pass the news, and the class is called method called Accept the message.

Now we can define: The package is hidden in the implementation of the contents and behaviors of things, and users don't need to know their internal implementation. This is a conclusion that a large number of programmers have obtained after repeated labor. This kind of benefit is to use convenience, easy to maintain, any kind of manual movement. Of course, we can't guarantee efficient, but don't mean that the encapsulation is not efficient. If you guarantee efficient on the basis of the package, I can't find a reason to reject him.

2001/8/12

Ding Ning

转载请注明原文地址:https://www.9cbs.com/read-28801.html

New Post(0)