Object-Oriented Programming, my thoughts (upper part)

Source: Internet
Author: User
Tags java format
Object-Oriented Programming, my thoughts

Preface:

This document aims to help my colleagues better understand object-oriented programming. This allows later users to take less detours, but many vulnerabilities and errors are inevitable. We also ask our predecessors to provide valuable suggestions for changes. After all, the Exchange conference will keep us updated.

Technology is changing with each passing day, and he will not wait for your growth. Technology must be used to communicate with others. Learning is a subjective understanding of yourself! The Exchange Conference makes progress faster. I think that if the computer architecture does not undergo revolutionary changes, the language of the program we are currently using will be a constant change! What do you learn about programming? Thoughts! After mastering a programming language (preferably an object-oriented language) and then engaging in other programming languages, you will find that the process is so cloudification! Why? You have mastered the idea of programming and learned other things. It is nothing more than learning a new syntax format.

I am not here to discuss with you how to use C ++ or Java, nor to discuss with you how to learn them. What I want to discuss with you is how to understand object-oriented. It mainly involves "classes, objects, inheritance, attributes, methods, static, overload, hiding, refactoring, declaration, definition, initialization, and assignment". There are many related technologies. generation after generation, this gives you a way To get started. For more detailed technical details, please refer to other related books for further research! Because I am only discussing with you how to better understand object-oriented!

How to Improve efficiency? Reuse resources and use other people's things. This is a good idea! For you, the greatest resource is confidence and enthusiasm! Well, let's join in the object-oriented programming for a better chance!

Note: I used Java to write all program instances in this article. Of course, it is similar in C ++. I will point it out in different places!

Note: The text in the article should be black, indicating that the text should be blue, the text should be highlighted in orange, and the text should be marked in red!

Body:

1. Basic concepts:

A Preliminary Study on 1.1 categories and objects:

I want to talk about process-oriented languages and object-oriented languages. The first thing I want to tell him is class and object! -------- "What is the world composed ?" Different people may have different answers to this question. If it's a chemist, he might tell you, "Are you still asking? The world is composed of molecules, atoms, ions, and other chemicals ". What if it's a painter? He may tell you, "the world is made up of different colors "....... Well, there are different opinions! But it would be much more interesting for a classifier to think about it. He will tell you that "the world is made up of different types of things and things! As an object-oriented programmer, we need to consider the problem from the perspective of the classifier! Yes, the world is composed of animals, plants, etc. Animals are divided into single-cell animals, multi-cell animals, and mammals. mammals are divided into humans, elephants, and tigers ...... That's all!

Now, from the abstract point of view, Let's define the "class! I mean, from an abstract point of view, you say, "What is human ?" First, let's take a look at some of the features of humans, including attributes (some parameters, values) and methods (some actions, what can they do !). Each person has attributes such as height, weight, age, and blood type. People will work, people will walk upright, people will use their own minds to create tools and so on! The reason why humans are different from other types of animals is that everyone has the attributes and methods of human beings. "Human" is just an abstract concept. It is just a concept. It is a non-existent entity! However, all objects with attributes and methods of the "human" group are called! This object "person" is actually an entity! Everyone is an object of the human group. Why are tigers not human beings? Because it does not have the attributes and Methods of the human group, the tiger does not walk upright, does not use tools, and so on! So tigers are not people!

The ------- class describes a group of objects with the same features (attributes) and same behavior (methods. In programs, classes are actually data types! For example, integer or decimal number. Integers also have a set of features and actions. The difference between a process-oriented language and an object-oriented language is that a process-oriented language does not allow programmers to define their own data types, but can only use the built-in data types in the program! To simulate the real world and better solve the problem, we often need to create the data types necessary to solve the problem! Object-Oriented programming provides us with solutions.

1.2 built-in data types and functions:

When storing data, a computer program must track three basic attributes:

1. Where information is stored;

2. What is the stored value;

3. What type of stored information;

Let's take a look at the built-in data types in the programming language! (Well, this is not easy to say, because each language has its own unique data type, but this is a minority, for example, there is byte data in Java, I don't have it in C ++. I hope you can give it a different picture !) For example, integer "int" and floating point data "float "! String "string", as well as arrays and struct. However, when writing a program, we will create a type of variable or constant as needed. For example, we can do this because we need to create an integer variable I as 5, int I = 5; as needed, I may change the I value, that is, assign a new value to it, for example, let it wait for 6, you can change it to I
= 6; from this we know that the amount that can change in the value is called a variable. The amount that will not change is called a constant. In C ++, the Count keyword is used for declaration, while in Java, the final keyword is used for declaration. Since the declaration formats in different languages are different, I will not describe them here. For details, refer to relevant books!

Here we will mainly discuss the function. We can think of the function as a "black box implementing a specific function" ------- this function is set by you. For example: now I ask you "how much is 2 + 3 equal "? I believe that you can answer me 5 quickly. Let's analyze the information contained in this sentence! First, I want to think of your brain as a black box. I don't know how your brain works (that is, how it operates ), I only care about what information I sent to you? What information have you done? And what information did you return to me? Note that each method returns a message to the caller, except the constructor (I will introduce it in detail later ). Now I need to think of myself as a programmer. What about you? Of course it's a computer! Computing can be less intelligent than people. It will only run in a specific format agreed in advance. I want it to have the function described above, so I need to define this black box first! First, I want to tell this black box that there will be two integers for you (this is the so-called parameter, which is the information the programmer needs to give to the Black Box ), then we need to define the black box to implement the addition of the two integers (this is the processing of the black box on the data, you can do any processing as needed .). At last, mark it and return it to me as an integer value (this is the information that the black box returns to the programmer ). A function is defined in this way. Let's look at the writing format:

Int addnum (int x, int y ){

Return X + Y;

}

The specific meaning is as follows:

INT/* return value type */addnum/* method (Black Box) name */(int x, int y/* input parameter */){

Return X + Y;/* internal method (to implement the addition operation) and return the return result to the caller */

}

First, pay attention to the "return" statement described above! The return keyword is used to return the information that follows the caller! Just like above, You will answer me only when I ask you. If I don't ask you, you don't have to answer me! In the same way in the computer, where can this function be called? I can only tell you where to call it! Of course, you can change parameters, return values, and internal implementations as needed. You have to refer to the relevant materials for details on how to define how to call them! Here I just give you a thought!

Sometimes you may encounter such a problem. I want you to remember that I am 20 years old! Literally, you didn't return me any information! However, in fact, you did return information to me. The content of the information is "no information, that is, no return value type void ". The specific procedure is as follows:

Int myage = 0;

Int A = 20;

Void remage (int ){

Myage =;

}

The specific functions are described as follows:

Int myage = 0; // define and initialize my age to 0;

Int A = 20;/* define variable A to 20 */

Void/* return value type: no return value type */remage/* function name */(int A/* input parameter */){

Myage = A; // internal implementation method. Note that no return is returned !!!

}

There are many other topics about functions. I will not introduce them one by one here. My goal is to let you know what the function is for a while! Pave the way for the following discussion!

1.3 pointer and reference:

Pointers and references exist in C ++, but are not in Java. Java removes the memory operation and removes the operator overload operation. However, I will introduce some functions of Operator Overloading later. References are mainly used to pass function parameters. So I will not introduce it too much here. They are very practical. If you are interested, you can refer to C ++ related books.

1.4 operators and control statements:

Let's take a look at the relevant books by yourself. I will not repeat them here!

2. in-depth exploration of object-oriented:

2.1 internal details of the "type:

With the above knowledge, we can now thoroughly explore the internal implementation of the class. All the knowledge points will be centered around classes and objects. Before that, I hope you will be able to fully understand the basic content described above!

Yes, the biggest feature of the object-oriented programming language is that you can write the data types you need to better solve the problem. I think I must help you figure out the relationships between classes, objects, attributes, and methods "! As I mentioned earlier, the human class cannot do anything, because "human" is just an abstract concept, and it is not a real "thing ", this "thing" is the so-called object. Only the "object" of a person can work. What about classes? Class is the description of the object! The object is generated from the class! In this case, the object has all the attributes and methods described by the class. ------- Be sure to understand this sentence !!!

Maybe you are a little overwhelmed. It doesn't matter! Let me give you another example! For example, TV sets have working principle diagrams. What is TV set? As long as it can implement all functional objects in the working principle diagram, we call it television. Do you think this is the case? However, the TV schematic cannot work, that is, the schematic cannot watch the program. Only the TV "entity-the so-called object" can watch the program. That is to say, after an object is generated from the class, it is truly meaningful! To start work. At this time, the TV has all the properties and methods described in the TV schematic! Okay, huh, huh!

As I mentioned earlier, classes are a set of attributes and methods. These attributes and methods can be declared as private, public, or protected. They describe access control for class members. Next I will introduce them separately:

1. Public: After declaring a variable as a public type, you can directly access it through an object. Everything is exposed! That is to say, your credit card password can be directly obtained by others.

2. Private: If the variable is declared as private, it will be much better. To get my credit card password, the object must call a dedicated method.

3. Protected (protected): We will discuss it again when introducing inheritance.

4. default access control character (friendly): // in Java, but not in C ++.

To encapsulate data and improve data security, we generally declare the class attributes as private and the class methods as public. In this way, the object can directly call all the methods defined in the class. When the object wants to modify or obtain its own attributes, it must call a defined dedicated method. Think about it. Will you publish your credit card password? Haha! Therefore, we advocate "Object adjustment methods and methods to change attributes ";

2.2 view memory allocation through instances:

Let's look at an instance! For example, if we want to write an employee management system for a company, what is the most suitable data type? I think it's an employee! However, in a process-oriented language, this is not allowed because it can only use internal data types in the language! Employees are not in this internal data type! Some people may say they can use struct in C! After all, it is the foundation of the class! If you used to be a C or B programmer, please forget this. Let's take a look at how to implement it with classes!

A company's employees are a special group of humans. In addition to all the features and methods of humans, it also has additional features and methods, for example, she has her salary, credit card password, schedule, and so on. These features, work content, workload, and so on. How can we define this class in a computer? Next I will write its format to show you what it looks like in a computer!

/* What I need to declare again here is that I use the Java format, which is different from the C ++ syntax! Many details and internal operations have many differences, but the thoughts are indeed similar */

// Employee. Java

Public class employee {

Private string name; // employee name

Private int age; // employee age

Private char sex; // employee gender

Private float emolument; // employee salary

Private Boolean lunch; // employee lunch

//...... And so on.

Public void heater () {// This method is used to process employees' lunch

Lunch = true;

}

Public void setname (string a) {// This method modifies the employee name

Name =;

}

Public String getname () {// This method is used to get the employee name

Return name;

}

//...... And so on.

}

In this way, the data types we need are defined. Now, let's see what it can do and how it works!

What I want to do is, there is a light pole commander in the studio named "Jingwei". I changed its name and output it to see how I did it!

Note: Observe how an object calls a method. It uses the "." operator! In fact, the "." operator is used when an object calls a public property or method.

However, if a pointer of the same type is defined in C ++, the "->" operator is used when the pointer calls the method of this object. For more details, see related books!

 

// Workstation. Java

Import java. AWT. graphics;

Import java. Applet. Applet;

Public class workstation extends applet {

Private employee Jingwei; // object declaration. No memory is allocated at this time!

Public void Init (){

Jingwei = new employee ();/* the constructor will be called when an object is created. Please wait for a while */

Jingwei. setname ("JW"); // set my name

}

Public void paint (Graphics g ){

G. drawstring ("My age is" + Jingwei. getname (), 10, 10); // display my age

}

}

The output result is:

My name is JW

The string is at the position where the X axis of the output window is 10 PX and the Y axis is 10 PX.

What I want to do now is to make a big anatomy of the above program so that you can see exactly what it is! (I can show you the Assembly from time to time. Oh, I won't do that either :)

First, let's take a look at our custom data type employee. In the application, it is no different from the int type data. In the same way, we need to create variables (objects ), however, the former is defined by ourselves, and then it is built in. The employee class has many attributes and many methods. At this time, we cannot directly use the created object to call its attributes for modification. Because it is private-protected! If I want to modify my name, I need to call the setname () method with an object, and if I want to get my name, I need to call the getname () method. We are completely walking by route. This route is "Object adjustment method, method change attribute"

Okay, I really believe you have understood what it is! Haha! Start your flight and move on!

Now let's take a look at the workstation class. This is a main class, similar to the main () function in C ++. In Java, only one primary class is allowed for a file. The primary class is declared as public! It must have the same main () function as C ++.

Let's take a look at the first statement in this class! Private employee Jingwei; the role of this statement is to declare the Jingwei object of an employee (no declaration is required in C ++ ). What I want to tell you is the difference between "Declaration" and "Definition. The declaration only tells the computer that there will be such a variable (object). In the memory, it does not allocate memory for this variable (object! The variable (object) is allocated memory only when defined. (It should be noted that the init () method is used to complete initialization. The object is defined here and memory is allocated to the object. The START () method is used to start the main thread of the browser, and the paint () method is used to display the apple interface. These are required by the applet, but they are not required by the application. Of course, they are not required in C ++. For more information, see related books)

Then we start to define an object. Therefore, operations on the Jingwei object will have practical significance. Never think like this: "trying to operate on the class !" As I said above, the principle of television is not to watch TV! This is meaningless! Let's look at this statement Jingwei = new employee (); it means to define a Jingwei object of the employee type. At this point, I want to tell you: "What does Jingwei have ". It has all the attributes and methods described by the class. I will list them for you one by one:

/* All employee objects have these attributes. Each time an object is created, a new memory is allocated to store these attributes of the object. I mean, each object has its own unique copy */

Private string name; // employee name

Private int age; // employee age

Private char sex; // employee gender

Private float emolument; // employee salary

Private Boolean lunch; // employee lunch

/* All employee objects have these methods. But there is only one copy in the memory */

Public void heater () {// This method is used to process employees' lunch

Lunch = true;

}

Public void setname (string a) {// This method modifies the employee name

Name =;

}

Public String getname () {// This method is used to get the employee name

Return name;

}

/* However, in fact, when the Jingwei object is created, the computer only allocates memory to all attributes of the object, but does not allocate memory to the method. There is only one method that belongs to all objects. Therefore, no matter how many objects are created, the computer will allocate only one piece of memory for one method. */

I 'd like to give you an example. Otherwise, you will not be able to faint. Haha!

Let's look at my statement "Private Boolean lunch;" for lunch, every employee needs to bring meals. We now think that the company's space is all the memory capacity, and your desk is part of the memory in the computer (each employee has a copy, which is allocated when the object is created ). You brought lunch to your company and put it on your desk. "Lunch" occupies the corner of your desk (occupies the memory capacity of your "object ). This lunch is only for you, and others belong to her! Therefore, every employee (object) needs a fast space (memory) to store their lunch (attribute ). This is also true in the computer. Every time an object is created, it will allocate a new memory in the memory to put the "lunch-lunch" attribute (all attributes of the object ). The computer only allocates memory for the properties of the object. Because each object is different! Just like the lunch you bring to your company is different from the lunch I bring to your company! But the method is different. The lunch in the morning is cold at noon. You need to use a microwave oven for heating. The microwave oven does not need to be carried by you. The company has a space (only a part of the company's space) and it is placed on the lunch desk. Who does the microwave oven belong? It belongs to all employees! Because every employee can use it. Instead of bringing one copy to every employee. It can be seen that each employee (object) has a lunch (attribute), but all employees (objects) only have one microwave oven (method ). All employees (objects) can use this microwave oven (method) to change the hot and cold states of their lunch (properties. The same way! This is also true in the computer. There is only one method for all objects! Attribute is a copy of each object, because each object has different attributes. Don't tell me that you still don't understand, or I will hit the wall, haha :)

 

Auto: http://blog.csdn.net/flashmagic/article/details/19999

Object-Oriented Programming, my thoughts

Preface:

This document aims to help my colleagues better understand object-oriented programming. This allows later users to take less detours, but many vulnerabilities and errors are inevitable. We also ask our predecessors to provide valuable suggestions for changes. After all, the Exchange conference will keep us updated.

Technology is changing with each passing day, and he will not wait for your growth. Technology must be used to communicate with others. Learning is a subjective understanding of yourself! The Exchange Conference makes progress faster. I think that if the computer architecture does not undergo revolutionary changes, the language of the program we are currently using will be a constant change! What do you learn about programming? Thoughts! After mastering a programming language (preferably an object-oriented language) and then engaging in other programming languages, you will find that the process is so cloudification! Why? You have mastered the idea of programming and learned other things. It is nothing more than learning a new syntax format.

I am not here to discuss with you how to use C ++ or Java, nor to discuss with you how to learn them. What I want to discuss with you is how to understand object-oriented. It mainly involves "classes, objects, inheritance, attributes, methods, static, overload, hiding, refactoring, declaration, definition, initialization, and assignment". There are many related technologies. generation after generation, this gives you a way To get started. For more detailed technical details, please refer to other related books for further research! Because I am only discussing with you how to better understand object-oriented!

How to Improve efficiency? Reuse resources and use other people's things. This is a good idea! For you, the greatest resource is confidence and enthusiasm! Well, let's join in the object-oriented programming for a better chance!

Note: I used Java to write all program instances in this article. Of course, it is similar in C ++. I will point it out in different places!

Note: The text in the article should be black, indicating that the text should be blue, the text should be highlighted in orange, and the text should be marked in red!

Body:

1. Basic concepts:

A Preliminary Study on 1.1 categories and objects:

I want to talk about process-oriented languages and object-oriented languages. The first thing I want to tell him is class and object! -------- "What is the world composed ?" Different people may have different answers to this question. If it's a chemist, he might tell you, "Are you still asking? The world is composed of molecules, atoms, ions, and other chemicals ". What if it's a painter? He may tell you, "the world is made up of different colors "....... Well, there are different opinions! But it would be much more interesting for a classifier to think about it. He will tell you that "the world is made up of different types of things and things! As an object-oriented programmer, we need to consider the problem from the perspective of the classifier! Yes, the world is composed of animals, plants, etc. Animals are divided into single-cell animals, multi-cell animals, and mammals. mammals are divided into humans, elephants, and tigers ...... That's all!

Now, from the abstract point of view, Let's define the "class! I mean, from an abstract point of view, you say, "What is human ?" First, let's take a look at some of the features of humans, including attributes (some parameters, values) and methods (some actions, what can they do !). Each person has attributes such as height, weight, age, and blood type. People will work, people will walk upright, people will use their own minds to create tools and so on! The reason why humans are different from other types of animals is that everyone has the attributes and methods of human beings. "Human" is just an abstract concept. It is just a concept. It is a non-existent entity! However, all objects with attributes and methods of the "human" group are called! This object "person" is actually an entity! Everyone is an object of the human group. Why are tigers not human beings? Because it does not have the attributes and Methods of the human group, the tiger does not walk upright, does not use tools, and so on! So tigers are not people!

The ------- class describes a group of objects with the same features (attributes) and same behavior (methods. In programs, classes are actually data types! For example, integer or decimal number. Integers also have a set of features and actions. The difference between a process-oriented language and an object-oriented language is that a process-oriented language does not allow programmers to define their own data types, but can only use the built-in data types in the program! To simulate the real world and better solve the problem, we often need to create the data types necessary to solve the problem! Object-Oriented programming provides us with solutions.

1.2 built-in data types and functions:

When storing data, a computer program must track three basic attributes:

1. Where information is stored;

2. What is the stored value;

3. What type of stored information;

Let's take a look at the built-in data types in the programming language! (Well, this is not easy to say, because each language has its own unique data type, but this is a minority, for example, there is byte data in Java, I don't have it in C ++. I hope you can give it a different picture !) For example, integer "int" and floating point data "float "! String "string", as well as arrays and struct. However, when writing a program, we will create a type of variable or constant as needed. For example, we can do this because we need to create an integer variable I as 5, int I = 5; as needed, I may change the I value, that is, assign a new value to it, for example, let it wait for 6, you can change it to I
= 6; from this we know that the amount that can change in the value is called a variable. The amount that will not change is called a constant. In C ++, the Count keyword is used for declaration, while in Java, the final keyword is used for declaration. Since the declaration formats in different languages are different, I will not describe them here. For details, refer to relevant books!

Here we will mainly discuss the function. We can think of the function as a "black box implementing a specific function" ------- this function is set by you. For example: now I ask you "how much is 2 + 3 equal "? I believe that you can answer me 5 quickly. Let's analyze the information contained in this sentence! First, I want to think of your brain as a black box. I don't know how your brain works (that is, how it operates ), I only care about what information I sent to you? What information have you done? And what information did you return to me? Note that each method returns a message to the caller, except the constructor (I will introduce it in detail later ). Now I need to think of myself as a programmer. What about you? Of course it's a computer! Computing can be less intelligent than people. It will only run in a specific format agreed in advance. I want it to have the function described above, so I need to define this black box first! First, I want to tell this black box that there will be two integers for you (this is the so-called parameter, which is the information the programmer needs to give to the Black Box ), then we need to define the black box to implement the addition of the two integers (this is the processing of the black box on the data, you can do any processing as needed .). At last, mark it and return it to me as an integer value (this is the information that the black box returns to the programmer ). A function is defined in this way. Let's look at the writing format:

Int addnum (int x, int y ){

Return X + Y;

}

The specific meaning is as follows:

INT/* return value type */addnum/* method (Black Box) name */(int x, int y/* input parameter */){

Return X + Y;/* internal method (to implement the addition operation) and return the return result to the caller */

}

First, pay attention to the "return" statement described above! The return keyword is used to return the information that follows the caller! Just like above, You will answer me only when I ask you. If I don't ask you, you don't have to answer me! In the same way in the computer, where can this function be called? I can only tell you where to call it! Of course, you can change parameters, return values, and internal implementations as needed. You have to refer to the relevant materials for details on how to define how to call them! Here I just give you a thought!

Sometimes you may encounter such a problem. I want you to remember that I am 20 years old! Literally, you didn't return me any information! However, in fact, you did return information to me. The content of the information is "no information, that is, no return value type void ". The specific procedure is as follows:

Int myage = 0;

Int A = 20;

Void remage (int ){

Myage =;

}

The specific functions are described as follows:

Int myage = 0; // define and initialize my age to 0;

Int A = 20;/* define variable A to 20 */

Void/* return value type: no return value type */remage/* function name */(int A/* input parameter */){

Myage = A; // internal implementation method. Note that no return is returned !!!

}

There are many other topics about functions. I will not introduce them one by one here. My goal is to let you know what the function is for a while! Pave the way for the following discussion!

1.3 pointer and reference:

Pointers and references exist in C ++, but are not in Java. Java removes the memory operation and removes the operator overload operation. However, I will introduce some functions of Operator Overloading later. References are mainly used to pass function parameters. So I will not introduce it too much here. They are very practical. If you are interested, you can refer to C ++ related books.

1.4 operators and control statements:

Let's take a look at the relevant books by yourself. I will not repeat them here!

2. in-depth exploration of object-oriented:

2.1 internal details of the "type:

With the above knowledge, we can now thoroughly explore the internal implementation of the class. All the knowledge points will be centered around classes and objects. Before that, I hope you will be able to fully understand the basic content described above!

Yes, the biggest feature of the object-oriented programming language is that you can write the data types you need to better solve the problem. I think I must help you figure out the relationships between classes, objects, attributes, and methods "! As I mentioned earlier, the human class cannot do anything, because "human" is just an abstract concept, and it is not a real "thing ", this "thing" is the so-called object. Only the "object" of a person can work. What about classes? Class is the description of the object! The object is generated from the class! In this case, the object has all the attributes and methods described by the class. ------- Be sure to understand this sentence !!!

Maybe you are a little overwhelmed. It doesn't matter! Let me give you another example! For example, TV sets have working principle diagrams. What is TV set? As long as it can implement all functional objects in the working principle diagram, we call it television. Do you think this is the case? However, the TV schematic cannot work, that is, the schematic cannot watch the program. Only the TV "entity-the so-called object" can watch the program. That is to say, after an object is generated from the class, it is truly meaningful! To start work. At this time, the TV has all the properties and methods described in the TV schematic! Okay, huh, huh!

As I mentioned earlier, classes are a set of attributes and methods. These attributes and methods can be declared as private, public, or protected. They describe access control for class members. Next I will introduce them separately:

1. Public: After declaring a variable as a public type, you can directly access it through an object. Everything is exposed! That is to say, your credit card password can be directly obtained by others.

2. Private: If the variable is declared as private, it will be much better. To get my credit card password, the object must call a dedicated method.

3. Protected (protected): We will discuss it again when introducing inheritance.

4. default access control character (friendly): // in Java, but not in C ++.

To encapsulate data and improve data security, we generally declare the class attributes as private and the class methods as public. In this way, the object can directly call all the methods defined in the class. When the object wants to modify or obtain its own attributes, it must call a defined dedicated method. Think about it. Will you publish your credit card password? Haha! Therefore, we advocate "Object adjustment methods and methods to change attributes ";

2.2 view memory allocation through instances:

Let's look at an instance! For example, if we want to write an employee management system for a company, what is the most suitable data type? I think it's an employee! However, in a process-oriented language, this is not allowed because it can only use internal data types in the language! Employees are not in this internal data type! Some people may say they can use struct in C! After all, it is the foundation of the class! If you used to be a C or B programmer, please forget this. Let's take a look at how to implement it with classes!

A company's employees are a special group of humans. In addition to all the features and methods of humans, it also has additional features and methods, for example, she has her salary, credit card password, schedule, and so on. These features, work content, workload, and so on. How can we define this class in a computer? Next I will write its format to show you what it looks like in a computer!

/* What I need to declare again here is that I use the Java format, which is different from the C ++ syntax! Many details and internal operations have many differences, but the thoughts are indeed similar */

// Employee. Java

Public class employee {

Private string name; // employee name

Private int age; // employee age

Private char sex; // employee gender

Private float emolument; // employee salary

Private Boolean lunch; // employee lunch

//...... And so on.

Public void heater () {// This method is used to process employees' lunch

Lunch = true;

}

Public void setname (string a) {// This method modifies the employee name

Name =;

}

Public String getname () {// This method is used to get the employee name

Return name;

}

//...... And so on.

}

In this way, the data types we need are defined. Now, let's see what it can do and how it works!

What I want to do is, there is a light pole commander in the studio named "Jingwei". I changed its name and output it to see how I did it!

Note: Observe how an object calls a method. It uses the "." operator! In fact, the "." operator is used when an object calls a public property or method.

However, if a pointer of the same type is defined in C ++, the "->" operator is used when the pointer calls the method of this object. For more details, see related books!

 

// Workstation. Java

Import java. AWT. graphics;

Import java. Applet. Applet;

Public class workstation extends applet {

Private employee Jingwei; // object declaration. No memory is allocated at this time!

Public void Init (){

Jingwei = new employee ();/* the constructor will be called when an object is created. Please wait for a while */

Jingwei. setname ("JW"); // set my name

}

Public void paint (Graphics g ){

G. drawstring ("My age is" + Jingwei. getname (), 10, 10); // display my age

}

}

The output result is:

My name is JW

The string is at the position where the X axis of the output window is 10 PX and the Y axis is 10 PX.

What I want to do now is to make a big anatomy of the above program so that you can see exactly what it is! (I can show you the Assembly from time to time. Oh, I won't do that either :)

First, let's take a look at our custom data type employee. In the application, it is no different from the int type data. In the same way, we need to create variables (objects ), however, the former is defined by ourselves, and then it is built in. The employee class has many attributes and many methods. At this time, we cannot directly use the created object to call its attributes for modification. Because it is private-protected! If I want to modify my name, I need to call the setname () method with an object, and if I want to get my name, I need to call the getname () method. We are completely walking by route. This route is "Object adjustment method, method change attribute"

Okay, I really believe you have understood what it is! Haha! Start your flight and move on!

Now let's take a look at the workstation class. This is a main class, similar to the main () function in C ++. In Java, only one primary class is allowed for a file. The primary class is declared as public! It must have the same main () function as C ++.

Let's take a look at the first statement in this class! Private employee Jingwei; the role of this statement is to declare the Jingwei object of an employee (no declaration is required in C ++ ). What I want to tell you is the difference between "Declaration" and "Definition. The declaration only tells the computer that there will be such a variable (object). In the memory, it does not allocate memory for this variable (object! The variable (object) is allocated memory only when defined. (It should be noted that the init () method is used to complete initialization. The object is defined here and memory is allocated to the object. The START () method is used to start the main thread of the browser, and the paint () method is used to display the apple interface. These are required by the applet, but they are not required by the application. Of course, they are not required in C ++. For more information, see related books)

Then we start to define an object. Therefore, operations on the Jingwei object will have practical significance. Never think like this: "trying to operate on the class !" As I said above, the principle of television is not to watch TV! This is meaningless! Let's look at this statement Jingwei = new employee (); it means to define a Jingwei object of the employee type. At this point, I want to tell you: "What does Jingwei have ". It has all the attributes and methods described by the class. I will list them for you one by one:

/* All employee objects have these attributes. Each time an object is created, a new memory is allocated to store these attributes of the object. I mean, each object has its own unique copy */

Private string name; // employee name

Private int age; // employee age

Private char sex; // employee gender

Private float emolument; // employee salary

Private Boolean lunch; // employee lunch

/* All employee objects have these methods. But there is only one copy in the memory */

Public void heater () {// This method is used to process employees' lunch

Lunch = true;

}

Public void setname (string a) {// This method modifies the employee name

Name =;

}

Public String getname () {// This method is used to get the employee name

Return name;

}

/* However, in fact, when the Jingwei object is created, the computer only allocates memory to all attributes of the object, but does not allocate memory to the method. There is only one method that belongs to all objects. Therefore, no matter how many objects are created, the computer will allocate only one piece of memory for one method. */

I 'd like to give you an example. Otherwise, you will not be able to faint. Haha!

Let's look at my statement "Private Boolean lunch;" for lunch, every employee needs to bring meals. We now think that the company's space is all the memory capacity, and your desk is part of the memory in the computer (each employee has a copy, which is allocated when the object is created ). You brought lunch to your company and put it on your desk. "Lunch" occupies the corner of your desk (occupies the memory capacity of your "object ). This lunch is only for you, and others belong to her! Therefore, every employee (object) needs a fast space (memory) to store their lunch (attribute ). This is also true in the computer. Every time an object is created, it will allocate a new memory in the memory to put the "lunch-lunch" attribute (all attributes of the object ). The computer only allocates memory for the properties of the object. Because each object is different! Just like the lunch you bring to your company is different from the lunch I bring to your company! But the method is different. The lunch in the morning is cold at noon. You need to use a microwave oven for heating. The microwave oven does not need to be carried by you. The company has a space (only a part of the company's space) and it is placed on the lunch desk. Who does the microwave oven belong? It belongs to all employees! Because every employee can use it. Instead of bringing one copy to every employee. It can be seen that each employee (object) has a lunch (attribute), but all employees (objects) only have one microwave oven (method ). All employees (objects) can use this microwave oven (method) to change the hot and cold states of their lunch (properties. The same way! This is also true in the computer. There is only one method for all objects! Attribute is a copy of each object, because each object has different attributes. Don't tell me that you still don't understand, or I will hit the wall, haha :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.