This is the 9th day of my participation in Gwen Challenge
Introduction: We have learned three major features of JAVA in the past, there are a lot of keywords in various knowledge points, many students are not very clear about the meaning of keywords, this chapter we will explain in detail several common keywords in object-oriented.
A, this:
1. Definition:
The this keyword is used to represent the current object itself, or an instance of the current class. This can be used to call all methods and attributes of the object, for example:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Demo1 {
public int num1=10;
public int num2=20;
public void sum(a){
// Use this to fetch the member variable
int num3 = this.num1 + this.num2;
System.out.println("num + num = " + num3);
}
public static void main(String[] args) {
Demo1 demo1=newDemo1(); demo1.sum(); }}Copy the code
Num1 +num2=30;
Num3 =num1+num2 = num3=demo1. Num1 +demo2. Num2 = 30.
2. Use:
This keyword has four main applications:
- This calls attributes of the class, that is, member variables of the class;
- This calls other methods in this class;
- This calls other constructors in the class, on the first line of the constructor.
Application 1: Reference a member variable:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student {
private String name;
public void setName(String name) {
this.name = name; }}Copy the code
In this code, we have wrapped the Student class basically. We have a member variable name, and we have a formal parameter name in the method. We pass the value of the formal parameter name to the member variable name, although we can see the meaning of this code. But how does it determine that as a Java compiler? Do you pass the value of the formal parameter name to the member variable name, or vice versa? That is, how does Java decide which variable to use if two variables have the same name? This keyword comes in handy. The this keyword represents a member variable or method in an object. That is, if you prefix a variable with the this keyword, it refers to the member variable or method of the object, not to the formal parameter or local variable of the member method. For this reason, in the above code, this.name represents the member variable of the object, also known as the property of the object, and the name behind it is the formal parameter of the method. The code this.name=name passes the value of the formal parameter to the member variable. That’s what this code is all about.
From this we can derive the syntax of applying member variables:
this.Member variablesCopy the code
Application 2: Call this class method:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student {
private String name;
public String getName(a) {
return name;
}
public void show(a){
System.out.println("Student information is as follows :"+this.getName()); }}Copy the code
In the above code, we encapsulated the attribute name and provided a public access method for the name attribute. In the later stage, other methods, such as a method to print student information, need to obtain the attribute name of the student first, so we can directly call the encapsulated getName method. Of course, here, our this can be omitted.
The syntax for calling member methods is as follows:
this. Membership method ();Copy the code
In general, member variables or member methods are referred to by object names in the Java language. Member variable or object name. The form of a member method. But some programmers like to use this even when they don’t have the same variable. References to variables in the form of member variables are intended to make code easier to read. As soon as you see the this keyword, you know that you are now referring to a member variable or method, not a local variable. This implicitly improves the readability of the code. However, this is the simplest use of the this keyword in the Java language. In this application, we can see that the this keyword represents the name of the object.
Application 3: Call the constructor:
The this keyword can call constructors as well as member variables. In a Java class, methods can be divided into member methods and constructor methods. A constructor is a method with the same name as a class, and a constructor must exist in a Java class. If no manifest constructor is shown in the code, the compiler automatically adds a constructor with no formal arguments at compile time. There are many differences between this constructor and normal member methods. The constructor does not return a value, and the void keyword is not used to indicate that the constructor does not return a value. A normal method may or may not have a return value, and the programmer can define it according to his or her needs. However, if a normal method does not return a value, be sure to use the void keyword in the method definition. Second, the constructor name is strictly the same as the class name. That is, the Java compiler recognizes that a method has the same name as a class and treats it as a constructor. For ordinary methods, they must not have the same name as the class, and multiple member methods must not have the same name. There can be multiple constructors in a class, all with the same name but with different formal parameters. The Java language determines which constructor to call based on formal arguments. The code is as follows:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student {
private Integer id; // Student id
private String name; // Attribute: name
private String address; // Attribute: address
// no argument constructor
public Student(a) {}// constructor with attribute name
public Student(String name) {
this.name = name;
}
// constructor with attribute id, name
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
// Constructor with attributes id,name,address
public Student(Integer id, String name, String address) {
this.id = id;
this.name = name;
this.address = address; }}Copy the code
In this code, we define multiple constructors with different parameters. Each constructor needs to pass in external parameters and assign values to member variables during initialization. Each constructor needs to perform the same process. The code is as follows:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student {
private Integer id; // Student id
private String name; // Attribute: name
private String address; // Attribute: address
// no argument constructor
public Student(a) {}// constructor with attribute name
public Student(String name) {
this.name = name;
}
// constructor with attribute id, name
public Student(Integer id, String name) {
this(name); // Call the constructor, equivalent to this.name=name, which must be on the first line of the method
this.id = id;
}
// Constructor with attributes id,name,address
public Student(Integer id, String name, String address) {
this(id,name); // Call the constructor, equivalent to this.id=id; This.name =name, which must be on the first line of the method
this.address = address; }}Copy the code
Constructors call each other using the following syntax:
this(property);Copy the code
There is a syntactic limitation, however, if you call the constructor in this way. In general, the constructor is called using the this keyword, only in the first sentence of a no-argument constructor that calls a constructor with arguments. Otherwise, there will be error messages when translating. This is different from referring to a member variable. When referring to a member variable, the this keyword is position-free. If you’re not familiar with this limitation, you might as well stick with the traditional constructor calls. It’s cumbersome, but at least it won’t go wrong.
Summary:
- The code that constructors call each other must be on the first line that calls the constructor.
- This () can only be in a constructor, not a normal method.