I. Differences between object-oriented and process-oriented
Process-oriented: the process and steps are taken into account. For example, if you are driving a car to Shenzhen, we need to consider how to proceed. That is to say: what to do
Object-oriented: it considers several objects. For example, when we drive to Shenzhen, we need to consider several objects, that is, how to do
Ii. Category
Concept: it is an object template. It is a set of objects with the same attributes and actions (methods ).
Differences between objects and classes: classes are abstract and objects exist.
Knowledge Extension: only one class in the same folder is public, and the main () method is absolutely included in the public class.
3. constructor and constructor)
Role: 1. It is mainly used to create objects. 2. It initializes member variables.
Features: 1. The class name must be the same. 2. There is no return value type, and void is not. 3. Each class has an implicit constructor, no parameters and method bodies 4. manually add an explicit constructor so that the implicit constructor will automatically disappear
Knowledge expansion: Object-oriented programming thinking steps: 1. Consider several objects. 2. identify several classes through objects. 3. Find the existing methods in each class. 4. Call the corresponding methods to implement functions.
For example, this simple example: public class test {
Public static void main (String [] args ){
Mouse mouse = new Mouse ();
Mouse. name = "mick ";
Cat cat = new Cat ();
Cat. name = "Tom ";
Cat. Catch (mouse );
}
}
Class Cat {
String name;
Public void Catch (Mouse mouse ){
Mouse. cry ();
}
}
Class Mouse {
String name;
Public void cry (){
System. out. println (name + "is crying ");
}
}
Way of Thinking: find two targets: tom and mick mice. Then we can find two classes: cat class and mouse class. Then, we can determine that the two objects contain two methods, respectively. One is the catch () of the tom object () method 1: cry () method of the mick object
Expert mode: if a method changes the state (attribute) of the object, then the method belongs
4. Four Regions of JVM (Java Virtual Machine)
1. code segment: stores bytecode.
2. heap segment: stores objects.
3. Stack segment: stores local variables.
4. static segment: stores string constants and static variables.
Difference between heap and stack: the stack can save large data types and run slowly. The stack can only save small data content but run fast.
Solution to the Problem of duplicate names of local variables and member variables
Method: this pointer is used in this case, because each object has a this pointer, which is equivalent to the reference of the current object. Shows the principle:
Figure 1
5. initialize member variables
1. in JAVA, member variables are initialized by default.
Value Type: The default value is 0 boolean. The default value is false. The default value is char. The default value is NULL.
Null Value Meaning: indicates that a reference does not point to any object, that is, the reference in the stack does not point to the memory address in any stack, for the image shown in, there is no reference arrow
2. Garbage collection mechanism (GC) in JAVA)
If an object does not point to any reference, this object is the garbage in JAVA, and JAVA will automatically call GC to automatically reclaim it.
Knowledge Extension: If a JAVA object needs to be reclaimed by GC after it is used, the best way to change it is to set the reference pointing to this object to NULL.
Vi. Static keywords
1. static member variables are called static variables (class variables)
The data in static segment cannot be repeated. All objects share their data. After an object is modified, the values of other objects also change.
Because all objects share it, it has nothing to do with the object, so it is also called a class variable. It can be called by class name. variable name.
Shows the logic:
Figure 2
The two objects in the heap share the Static variable Corcol in the Static segment. To change the value, you only need to use Person. Corcol to change the value.
2. The static method is called a static method (class method)
Class methods do not need to be called by creating objects. They can be called directly by class name. method name.
Note: (1) static methods can only access static member variables.
(2) non-static methods can access static member variables or non-static member variables.
Knowledge Extension: Why is the main () method static?
Because the main () method is the entry of the program, no object can be called before the main () method is used, so the main () method must be static.
All of the above are my summary. If you have any mistakes, please criticize and correct them.