One. this keyword
This points to its own reference, which is the object where the current method resides. One of its main functions is to pass the object itself as a parameter to a method in another object.
Usage One: Use this to make a distinction when the member variable has the same name as a local variable
/*** * When a member variable has the same name as a local variable, use this to differentiate * This represents the current object's variable *@authorhlwyf_000 **/ Public classTestthis {intAge ; Public voidChangeage (intAge ) { This. Age =Age ; } Public Static voidMain (string[] args) {testthis testthis=NewTestthis (); Testthis.age= 30; Testthis.changeage (45); System.out.println (Testthis.age); } }
Run Result: 45
In the next code, if you don't use age for this.age, you won't be able to distinguish between assigning values and giving yourself a situation.
With This.age, this represents the current object, so This.age is the age of the current object, which distinguishes it.
Public void changeage (int age ) { this. Age = Age ; }
Usage two: Passing itself as an object to other methods
It looks messy and takes a closer look at how it works.
Design Pattern: User mode
1 /***2 * Pass your own object as a parameter to other methods3 * @authorhlwyf_0004 *5 */6 Public classThstThis2 {7 Public Static voidMain (string[] args) {8A A =NewA ();9 }Ten } One A classa{ - PublicA () { -b b =NewB This);//here This represents the A object, the whole a is passed in! In B You can use a method the b.print (); - } - Public voidPrintA () { -System.out.println ("Print of a!"); + } - } + A classb{ at A; - PublicB (A a) { - This. A = A;//This represents the B object - } - Public voidprint () { -System.out.println ("A"); in This. A.printa (); //can also be used here A.printa (); - } to +}
Operation Result:
aprint of A!
Problem Analysis: Remove a in class B; What will it be? The code is as follows:
Modify Class B to:
1 classb{2 PublicB (A a) {3 A.printa ();4 }5 Public voidprint () {6System.out.println ("A");7 //A.printa (); 8 }9 Ten}
Operation Result:
Print of A! A
Normal!
Remove Comments:
class b{ public B (a a) { a.printa (); } Public void print () { System.out.println ("A"); A.printa (); } }
Result Error! Why is that?
Analysis: A in public B (a a) is a formal parameter assigned to a local variable, placed in the memory of the stack area, when the constructor of this method is completed, its life cycle disappears, then the memory is released. So if you use parameter A again, Because in memory no longer exists, so cannot call, compiler Error! The solution to this problem is as shown in the code above, which defines a member variable in the class, and assigns the value of the parameter to the member variable (note: the member variable type within a class can be a class, an array, an interface, as mentioned in the previous note), and the disappearance of member variables, such as the garbage collector, When the memory is full, it rarely happens.
The correct code is as follows:
class b{ // define member variable public B (a a) { a.printa (); } Public void print () { System.out.println ("A"); This . A.printa (); }}
View Code
Two. The difference between a class variable and an instance variable
Note: Static belongs to the class, not to the object! is not a member property and belongs to the static zone in memory!
/** error-prone knowledge points:
Public class Person { int : Age ; Static String classer;} Public class Mathod { }
In the same file, two public classes cannot be defined, only one public class can be defined, and in the Mathod class, it is correct to remove public.
****/
code Example Analysis:
1 Public classPerson {2 intAge//member variables, placed in memory in the heap area3 StaticString Classer;//class variables, placed in memory in the static zone4 }5 6 classMathod {7 Public Static voidMain (String args) {8 //defining two Person class objects9Person Person1 =NewPerson ();TenPerson Person2 =NewPerson (); One APerson1.age = 1; -Person2.age = 2; - thePerson1.classer = "abc!"; - System.out.println (person1.classer); - System.out.println (person2.classer); - } + -}
Preliminary compilation, unable to compile through, find the cause, find:
1. The file name must be the same as the public class name.
2. The compiler enters from the main function entry of the public class and does not compile if there is no main function. So, these errors are made in the above code.
Modify the following:
1 classPerson {2 intAge//member variables, placed in memory in the heap area3 StaticString Classer;//class variables, placed in memory in the static zone4 }5 6 Public classMathod {7 Public Static voidMain (string[] args) {8 //defining two Person class objects9Person Person1 =NewPerson ();TenPerson Person2 =NewPerson (); One APerson1.age = 1; -Person2.age = 2; - thePerson1.classer = "abc!"; - System.out.println (person1.classer); - System.out.println (person2.classer); - } + -}
Compilation Result:
Abc! ABC!
As you can see from here, the output is the same in lines 16 and 17. Because, classer in the static area of memory, for the local variables in the stack person1 and person2 can be called!
The memory allocation diagram for this code is as follows:
This is the difference between a class variable and an instance variable!
Objects are created through classes, so you need to load the appropriate classes when you create the objects. A class can create multiple objects, and each object has its own entity.
Difference:
1. Class variables (such as classer above) allocate memory when the class loads, instance variables (such as age above) are allocated memory when the object is created, class variables free memory when the program exits, the object does not exist, and the instance variable does not exist.
2. Class variables are shared by all objects, that is, objects of the same class share such class variables, instance variables are unique to each variable, and manipulating their own instance variables does not affect other objects.
3. Class variables are generally called by the class name.
"Learning Notes" Java Object-oriented programming 2