this basic usage

      In Java, this is the keyword for calling the variable in the class and the constructor of the inner class. When the object has a variable with the same name, the variable of the class can be specified.

Example 1:

package example_1;
import java.lang.*;

public class Xample_3  {
	private static String a;
	private static String b;
	public Xample_3(){
	}
	public Xample_3 (String a,String B){
		this.a = a ;
		b = B;
		/*
		 * In a constructor parameter, if the name of the parameter is the same as the variable name in the class, you need to use this to indicate the variable used.
		 * Here the variable a and the formal parameter a have the same name, so when calling, you need to use this.a to specify the a that uses the variable
		 * The variable b and the formal parameter B have different names, so the variable b can be used directly
		 */
		System.out.println("Reassign the a and b variables to the value in the construction class, a is equal to: "+this.a+"\nb is equal to: "+b);			
	}
	public String getA(){
		return a;	
	}
	public String SetA(String a){	
		return this.a=a;
	}
	public void setAandB(String a,String b){	
	}
        public static void main(String[] args) {
        	Xample_3 xample = new Xample_3();
        	Xample_3 xample_3 = new Xample_3("a1", "b1");
        	xample_3.SetA("aaa1");
        	System.out.println(xample_3.getA());
        	
        }
}
operation result:

In the construction class, the a and b variables are reassigned to the value, a is equal to: a1
   b is equal to: b1
aaa1




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726872&siteId=291194637