Java this keyword explanation
This keyword is used to represent the current object itself, or an instance of the current class. Through this, all methods and properties of this object can be called. For example:
public class Demo{ public int x = 10; public int y = 15; public void sum(){ // Fetch member variables through this point 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 above program, obj is an instance of the Demo class. This is equivalent to obj. Execution of int z = this.x + this.y; is equivalent to executing int z = obj.x + obj.y;.
Note: this only makes sense after class instantiation.
Use this to distinguish variables with the same name
When a member variable is duplicated with variables inside a method, you hope to call member variables inside the method. What should you do? Only this can be used at this time, 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 sa y(){ System.out.println("Website The name is "+ name + ", it has been established" + age + "year"); } public static void main(String[] args) { Demo obj = new Demo("WeChatyuan", 3); obj.say( ); }}
Running results:
The website's name is Weixueyuan, which has been established for 3 years.
The scope of formal parameters is the entire method body and is a local variable. In Demo(), formal parameters and member variables are duplicated. If this is not used, the local variable name and age are accessed, rather than member variables. In say(), we do not use this because the scope of the member variable is the entire instance, of course this can be added:
public void says(){ System.out.println("The name of the website is "+ this.name + ", it has been established" + this.age + "year");}
Java associates all member variables and member methods with this by default, so using this is redundant in some cases.
Initialize the object as a method name
That is, it is equivalent to calling other constructors of this class, which must be used as the first sentence of the constructor. Examples are as follows:
public class Demo{ public String name; public int age; public Demo(){ this("WeChatyuan", 3); } public Demo(String name, int age){ this.name = name ; this.age = age; } public void says(){ System.out.println("The name of the website is " + name + ", it has been established" + age + "year"); } public static void main(String[] args) { Demo obj = new Demo(); obj.say(); }}
Running results:
The website's name is Weixueyuan, which has been established for 3 years.
It is worth noting:
Another constructor is called in the constructor, and the call action must be placed at the starting position.
A constructor cannot be called within any method other than a constructor.
Only one constructor can be called within a constructor.
The above code involves method overloading, that is, Java allows multiple methods of the same name to appear, as long as the parameters are different. Subsequent chapters will be explained.
Passed as parameters
When a method needs to be called in some completely separated classes and a reference to the current object is passed 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!
Anonymous objects are objects without names. If the object is used only once, it can be used as an anonymous object. In the code, new B(this).print(); is equivalent to ( new B(this) ).print();, first create a non-existent object through new B(this) The object of name, then call its method.
Java method overloading <br />In Java, multiple methods in the same class can have the same name as long as their parameter list is different. This is called method overloading.
Parameter list is also called parameter signature, including the type of parameters, the number of parameters and the order of parameters. As long as there is a difference, it is called the parameter list different.
Overloading is a basic feature of object-oriented.
Let’s see a detailed example below.
public class Demo{ // A normal method without parameter void test(){ System.out.println("No parameters"); } // Overload the above method and bring an integer parameter void test( int a){ System.out.println("a: " + a); } // Overload the above method and bring two parameters void test(int a,int b){ System.out.println(" a and b: " + a + " " + b); } // Overload the above method and bring a double precision parameter double test(double a){ System.out.println("double a: " + a ); return a*a; } public static void main(String args[]){ Demo obj= new Demo(); obj.test(); obj.test(2); obj.test(2,3); obj .test(2.0); }}
Running results:
No parametersa: 2a and b: 2 3double a: 2.0
Through the above example, readers can see that overloading is a function with the same function name but different formal parameters in a class. The result of overloading can minimize the types of code and methods for a program block.
illustrate:
Rules for overloading of methods:
Implementation of method overloading:
When the method names are the same, the compiler will match the number of parameters, parameter types, etc. of the calling method to select the corresponding method. If the match fails, the compiler will report an error, which is called overload resolution.