The column " Java Zero-Basic Introduction to Mastery " is continuously updated. Through this column, you will learn a complete set of Java content from entry to advanced to actual combat , and all the content will be concentrated in this column. Both beginners and experienced developers can benefit from this column.
After subscribing to the column, add me on WeChat or enter the communication group . When you enter the group, you can find me to receive a full set of video courses such as front-end/Java/big data/Python/low-code/cloud native, and discuss problems together and work together~
Construction method
Construction methods are also called constructors, constructors, builders, etc. The role of the constructor is to assign initial values to the member variables in the class and realize reuse. Note that the constructor must 与类同名
, and 无返回值类型
(the void keyword is not required). The following is an example of a constructor, which is written in a class alongside member variables:
//类student
class student{
String name;
int age;
String address;
//构造方法
student(){
...
}
}
Then assign initial values to member variables in the function body of the constructor;
//类Student
class Student{
String name;
int age;
String address;
//构造方法
Student(String name1,int age1,String address1){
name = name1;
age = age1;
adress = address1;
}
}
Next, create an object and call the constructor. Student() after the new keyword is calling the constructor. When creating an object, pass in the corresponding parameter value, no matter how many objects there are, the syntax is the same;
//类Student
class Student{
String name;
int age;
String address;
//构造方法
Student(String name1,int age1,String address1){
name = name1;
age = age1;
address = address1;
}
}
//创建对象调用构造方法
Student zhangsan = new Studen("张三",23,"山西太原");
Student lisi = new Studen("李四",23,"山西运城");
...
In the following practical case, we first create a class Student , including member variables, methods and construction methods;
Then create a class ConsDemo , create an object and call the constructor in Student .
this keyword
This is used 指代当前对象
, that is to say, which object calls the method this refers to which object. The this keyword is introduced in Chapter 5 of the fourth edition of "Java Programming Thoughts" as follows: the this keyword can only be used inside a method, and it means a reference to "the object that called the method".
Here we need to know that the variables in the class are member variables , and the variables in the methods in the class are local variables . Local variables are only applicable in this method, while member variables can be used in any method in this class, the premise is to add this.
the keyword .
When the this keyword is used, the new constructor of class Student looks like this;
//类Student
public class Student {
//成员变量
String name;
int age;
String address;
//构造方法
Student(String name,int age,String address){
//将此处的变量(局部变量)赋值给成员变量,this不可省略
this.name = name; //此处的this.name就是指成员变量name
this.age = age;
this.address = address;
}
//方法
void study(){
System.out.println(this.name+"在学习!"); //此处的this可省略,因为它默认指的是成员变量
}
void myself(){
System.out.println("大家好,我是"+this.name+",今年"+this.age+"岁了,我在"+this.address);
}
}
When using a member variable in a method and the member variable has the same name as the local variable, the this keyword cannot be omitted, and it can be omitted in other cases.
Recommended in this issue
- Book delivery in this issue: "PyTorch Tutorial: 21 Projects to Play with PyTorch in Actual Combat"
- Book Donation Event: Please click here for event details