2. In-depth explore object-oriented:
2.1 "Type" internal details:
With the above knowledge, we can now implement the internal implementation of deep excavation classes. All knowledge points I will start with the object, before this, I hope that you can confirm that the basic content introduced to the above is completely mastered!
Yes, the biggest feature of object-oriented programming language is to write your own data type to better solve problems. I think I have to help you engage in the relationship between class, object, attribute, method! As I said earlier, the "class" is not doing, because "human beings" is just an abstract concept, it is not a real "thing", and this "thing" is the so-called object. Only people "objects" can work. And what? Class is a description of the object! Objects are generated from the class! At this point, the object has all the properties and methods described by the class. ------- Be sure to understand this sentence! ! !
Maybe you have already somewhat, it's okay! Good aftertaste, I will give an example! For example, televisions, TV sets have a working schematic, then what is TV? As long as it enables the object of all the features of the working principle, we call it TV. Do you think about this? However, the schematic of the TV is not working, that is, this schematic cannot watch the show, only the TV "entity - the so-called object" can watch the show, that is, after the class generates an object, Is true meaning! Can you start working. At this point, the TV has all the properties and methods described by TV schematic! Understand, huh, huh!
I have previously introduced that the class is a collection of attributes and methods. These attributes and methods can be declared as private, public or protected, which describes access control to class members. Let me do it separately:
1. Public (public): After the variable is declared as a common type, then you can directly access the object, everything is exposed! That is, your credit card password is also able to get directly.
2. Private (Private): If you declare the variable as a private situation, you will get my credit card password, and the object must call a dedicated method to get.
3. Protected (protected): Introduce the inheritance discussed.
4. Default Control Access (Friendly): // Nothing in C in Java.
In order to achieve data package, improve data security, we generally declare the class's attributes as private, and declare the method of the class as public. Thus, the object can directly call all the methods defined in the class, and you must call to define a good dedicated method when you want to modify or get your own properties. You think, will you publish your credit card password? Ha ha! Therefore, we advocate that "object adjustment method, method change attribute";
2.2 Look at memory allocation by example:
Said so much, let's take a look! For example: Now we have to write a company employee management system, what do you think is the most suitable data type? I think it is an employee! However, in the process-oriented language, this is not allowed because it can only use the internal data type in the language! And employees are not within this internal data type! Maybe someone will say that you can use the Struct in the C language! After all, it is the foundation of the class! If you are previously a programmer of C or B, please forget these, let's take a look at how to use classes to achieve this! An employee of a company is a special group of human beings. In addition to all of the characteristics and methods of humanity, it has additional features and methods, such as her salary, credit card password, schedule, etc., these features and Work content, workload, etc. These methods. And how should we define this class in your computer? Below I will write its format, let you see what it looks like in your computer!
/ * Here I need to declare again, I use the Java format, which is very different from C on the grammatical format! Many details and internal operations have a lot of differences, but in terms of thinking.
//employee.java
PUBLIC CLASS EMPLOYEEEE {
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 many more
Public void heater () {// This method is to process the lunch for processing employees
Lunch = true;
}
Public void setname (String a) {// This method is to modify the name of the employee
Name = a;
}
Public string getName () {// This method is to get the employee's name
Return Name;
}
//……and many more
}
This way we define the data type we need. Now let's take a look at what it can do and how to work!
What I want to do is that there is a light rod commander in the studio, "jingwei", I modified its name and output it, see how I did it!
Note: Please observe how the object calls the method, it uses "." Operator! In fact, this is the case when the object calls a common attribute or method.
However, in C , if a similar type of pointer is defined, the "->" operator is used when the pointer is called. More detailed content clear refers to the relevant books!
//workstation.java
Import java.awt.graphics;
IMPORT JAVA.Applet.applet;
Public class workstation extends applet {
Private Employee Jingwei; // The statement of the object, does not assign memory at this time!
Public void init () {
Jingwei = new Employee (); / * Create an object at this time calling the constructor, later introduced * /
Jingwei.setname ("jw"); // Set my name
}
Public void paint (graphics g) {
g.drawstring ("My Age IS" jingwei.getname (), 10, 10); // Show my age}
}
The output result is:
My Name is JW
This string is a position where the X coordinate axis of the output window is 10 PX, the Y coordinate axis is 10 PX.
What I have to do now is to make a big anlyiaque to make the above program, let you see how it is, how is it! (I will take you to the compilation inside, huh, I will not :)