1. Class Definition [Class modifier] class classname { // Content of the class body, member variables + member Methods } Note: The class modifier can only be public or non-modifier. If it is public, the file name saved by the class must be the same as that saved by the class name (it is recommended to keep it all the time) 2. member variables and local variables 1) member variables are valid within the class, and local variables are valid only within the method that defines them. 2) member variable classification: Static modification of instance member variables and class member variables) 3) member variable hiding, this keyword When the defined local variable and the member variable name are the same, you must use this to reference the member variable within the scope of the local variable. 3. Member Methods [Accesstype] returntype name ([parameter]) { // Method body } Accesstype: modifier, method access permission, can be omitted Accesstype: return type Parameter: Parameter Method overloading: A class contains several methods with the same name, but their parameters are different (type or number). The return type cannot be used to differentiate the overloading. 4. Constructor The constructor is used to create an object and complete initialization. 1) The name is exactly the same as the class name 2) No response type, and no void 3) There can be multiple constructor methods or no Constructor (with the default constructor) 5. Main Method Public static voidmain (string ARGs []) { //..... } Except that the method parameter name ARGs can be changed, the items here must be identical. The execution of a Java application starts from the main method. The string array of parameters can pass parameters to the application, which is similar to that of C. Public classmaindemo { Public static void main (string ARGs []) { For (INT I = 0; I <args. length; I ++) System. Out. println (ARGs [I]); } } Java maindemo first parameter second parameter third parameter The running result will output these three strings respectively.
6. Create, use, and clear objects Object declaration: Class Name Object Name; Allocate memory for objects: New Usually written together, such as circle circle1 = new circle (); Different objects have their own instance variables and instance methods, but they all have class variables. Object clearing: Java provides an automatic garbage collection mechanism Finalize () and GC () Finalize () of the object class ()
Protected void |
Finalize() When the Garbage Collector determines that there is no more reference to this object, the object's garbage collector calls this method. |
The Java garbage collection collector is not controlled by programmers, and can be implemented without rules. It does not necessarily wake up when garbage is generated. Therefore, this is not a reliable mechanism, we cannot guarantee that the finalize () method of each object will be called (finalize is called by the system) The system. GC () method forces the Garbage Collector to recycle the garbage. Public static voidGC() Calling the GC method implies that Java virtual machines have made some efforts to recycle unused objects so that they can quickly reuse the memory currently occupied by these objects. When the control is returned from the method call, the virtual machine has done its best to reclaim space from all discarded objects. Anonymous object: An anonymous object is an object that is not specified when an object is generated. There are two scenarios for using an anonymous object: 1) if you call an object only once, you can use an anonymous object, as shown in the preceding example. New person (). Cry (); 2) Passing anonymous objects as parameters to a function If you have the following methods: Public void getname (person P) { P. Cry (); } You can call the method obj. getname (new person () in this way ()); 7. Passing PARAMETERS IN THE METHOD Pass by value Void method (int I, intj) { I = I * 2; J = J * 2 } Int; Int B; After method (a, B) is called, the values of A and B do not change. Pass by reference A method parameter is an object. When a method is called, the object operates on the member variables of the object. After the call, the member variables of the object change; Example: Void method (methoddemoobj) { OBJ. A = 2 * obj.; OBJ. B = 2 * obj. B; |