编程知识 cdmana.com

Java this keyword

this Keywords are used to represent the current object itself , Or an instance of the current class , adopt this You can call all the methods and properties of this object . for example : public class Demo{

public int x = 10;
public int y = 15;

public void sum(){
    //  adopt  this  Click the member variable 
    int z = this.x + this.y;
    System.out.println("x + y = " + z);
}
public static void main(String[] args) {
    Demo obj = new Demo();
    obj.sum();
}

} Running results : x + y = 25

In the program above ,obj yes Demo An instance of a class ,this And obj Equivalent , perform int z = this.x + this.y;, It's like execution int z = obj.x + obj.y;.

Be careful :this It makes sense only after the class is instantiated . Use this Distinguish variables with the same name When a member variable has the same name as a variable inside a method , You want to call a member variable inside a method , What shall I do? ? You can only use this, for example : public class Demo{

public String name;
public int age;

public Demo(String name, int age){
    this.name = name;
    this.age = age;
}

public void say(){
    System.out.println(" What is the name of the website " + name + ", It has been established " + age + " year ");
}

public static void main(String[] args) {
    Demo obj = new Demo(" Micro school ", 3);
    obj.say();
}

} Running results : The name of the website is weixueyuan , It has been established 3 year

The scope of the formal parameter is the whole method body , It's a local variable . stay Demo() in , Formal parameter and member variable have the same name , If not used this, Access to local variables name and age, Instead of member variables . stay say() in , We didn't use this, Because the scope of the member variable is the entire instance , Of course, you can add this: public void say(){

System.out.println(" What is the name of the website " + this.name + ", It has been established " + this.age + " year ");

} Java By default, all member variables and member methods are associated with this Link together , Therefore use this In some cases, it is redundant . Initialize the object as the method name That is to say, it is equivalent to calling other construction methods of this class , It has to be the first sentence of the construction method . Examples are as follows : public class Demo{

public String name;
public int age;

public Demo(){
    this(" Micro school ", 3);
}

public Demo(String name, int age){
    this.name = name;
    this.age = age;
}

public void say(){
    System.out.println(" What is the name of the website " + name + ", It has been established " + age + " year ");
}

public static void main(String[] args) {
    Demo obj = new Demo();
    obj.say();
}

} Running results : The name of the website is weixueyuan , It has been established 3 year

It is worth noting that : Another construction method is called in the construction method , The calling action must be placed in the most initial position . Constructor cannot be called inside any method other than constructor . Only one constructor can be called within a constructor .

The above code involves method overloading , namely Java Multiple methods with the same name are allowed , As long as the parameters are different . The following chapters will explain . Pass as a parameter You need to call a method in some completely separated classes , And pass a reference to the current object as a parameter . for example : public class Demo{

public static void main(String[] args){
    B b = new B(new A());
}

}

class A{

public A(){
    new B(this).print();  //  Anonymous object 
}
public void print(){
    System.out.println("Hello from A!");
}

}

class B{

A a;
public B(A a){
    this.a = a;
}
public void print() {
    a.print();
    System.out.println("Hello from B!");
}

} Running results : Hello from A! Hello from B!

An anonymous object is an object without a name . If the object is used only once , It can be used as an anonymous object , In the code new B(this).print(); Equivalent to ( new B(this) ).print();, Through the first new B(this) Create an object without a name , Call its method again .this Keywords are used to represent the current object itself , Or an instance of the current class , adopt this You can call all the methods and properties of this object . for example : public class Demo{

public int x = 10;
public int y = 15;

public void sum(){
    //  adopt  this  Click the member variable 
    int z = this.x + this.y;
    System.out.println("x + y = " + z);
}

public static void main(String[] args) {
    Demo obj = new Demo();
    obj.sum();
}

} Running results : x + y = 25

In the program above ,obj yes Demo An instance of a class ,this And obj Equivalent , perform int z = this.x + this.y;, It's like execution int z = obj.x + obj.y;.

Be careful :this It makes sense only after the class is instantiated . Use this Distinguish variables with the same name When a member variable has the same name as a variable inside a method , You want to call a member variable inside a method , What shall I do? ? You can only use this, for example : public class Demo{

public String name;
public int age;

public Demo(String name, int age){
    this.name = name;
    this.age = age;
}

public void say(){
    System.out.println(" What is the name of the website " + name + ", It has been established " + age + " year ");
}

public static void main(String[] args) {
    Demo obj = new Demo(" Micro school ", 3);
    obj.say();
}

} Running results : The name of the website is weixueyuan , It has been established 3 year

The scope of the formal parameter is the whole method body , It's a local variable . stay Demo() in , Formal parameter and member variable have the same name , If not used this, Access to local variables name and age, Instead of member variables . stay say() in , We didn't use this, Because the scope of the member variable is the entire instance , Of course, you can add this: public void say(){

System.out.println(" What is the name of the website " + this.name + ", It has been established " + this.age + " year ");

} Java By default, all member variables and member methods are associated with this Link together , Therefore use this In some cases, it is redundant . Initialize the object as the method name That is to say, it is equivalent to calling other construction methods of this class , It has to be the first sentence of the construction method . Examples are as follows : public class Demo{

public String name;
public int age;

public Demo(){
    this(" Micro school ", 3);
}

public Demo(String name, int age){
    this.name = name;
    this.age = age;
}

public void say(){
    System.out.println(" What is the name of the website " + name + ", It has been established " + age + " year ");
}

public static void main(String[] args) {
    Demo obj = new Demo();
    obj.say();
}

} Running results : The name of the website is weixueyuan , It has been established 3 year

It is worth noting that : Another construction method is called in the construction method , The calling action must be placed in the most initial position . Constructor cannot be called inside any method other than constructor . Only one constructor can be called within a constructor .

The above code involves method overloading , namely Java Multiple methods with the same name are allowed , As long as the parameters are different . The following chapters will explain . Pass as a parameter You need to call a method in some completely separated classes , And pass a reference to the current object as a parameter . for example : public class Demo{

public static void main(String[] args){
    B b = new B(new A());
}

}

class A{

public A(){
    new B(this).print();  //  Anonymous object 
}
public void print(){
    System.out.println("Hello from A!");
}

}

class B{

A a;
public B(A a){
    this.a = a;
}
public void print() {
    a.print();
    System.out.println("Hello from B!");
}

} Running results : Hello from A! Hello from B!

An anonymous object is an object without a name . If the object is used only once , It can be used as an anonymous object , In the code new B(this).print(); Equivalent to ( new B(this) ).print();, Through the first new B(this) Create an object without a name , Call its method again .

This article altogether 1204 Number of words , Average reading time ≈ 4 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[await]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909125824190f.html

Tags java keyword
Scroll to Top