I. When can't this be omitted? Let's give an example to illustrate
class User2{ private int id; public int getId() { return id; } public void setId(int id) { this.id = id;//If it's written here id=id,Then according to the principle of proximity, the first id It's the formal parameter. The second one. id Also form //Parameter, so we have to add this Just like python Medium self } }
When this is used to distinguish local variables from instance variables, it cannot be omitted.
Second, we practice the construction method again and pay attention to our notes, which is an important summary of this connection.
package com.bjpowernode.java_learning; public class D42_ { public static void main(String[] args) { User2 u1 = new User2(2155,"Zhang San"); //Here are two questions //The prime minister can pass in parameters, indicating that this is a call User2 Class. You can see that the function prototype needs two parameters. //If we don't write these parameters, we need to User2 Add construction method without parameters (i.e. overloaded function) in //although id and name yes private It is of type, but we can see that the input parameters can still be modified, but we will modify later. //You have to call those two instance functions. System.out.println(u1.getName()); } } class User2{ private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id;//If it's written here id=id,Then according to the principle of proximity, the first id It's the formal parameter. The second one. id Also form //Parameter, so we have to add this Just like python Medium self } public String getName() { return name; } public void setName(String name) { this.name = name; } public User2(int id,String name) { this.id = id; this.name = name; } } //3. We need to create a Date class to store our Date. At the same time, there is a requirement: if we do not provide the Date of the year when we create an object, we need to give the object a default value of 1970.-01-01 package com.bjpowernode.java_learning; public class D42_ConstructerWithDefaultValue { public static void main(String[] args) { Date2 d1 = new Date2(2019,10,19); Date2 d2 = new Date2(); d1.outputDate(); d2.outputDate(); } } class Date2{ int year; int month; int day; /** * @param year * @param month * @param day */ public Date2(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } /** * Requirement: if you don't provide date when creating an object, we need to give this object a default value of 1970-01-01 */ public Date2(){ this(1970,1,1); } public void outputDate() { System.out.println("The date you want is:" + year + "year" + month + "month" + day + "day"); } }
Let's take a look at two constructors. One is that we can pass in parameters. The other is that we don't pass in parameters. There is a fixed format: this (the default parameter), and this statement must appear in the first line of this construction method. What other statements are written later?
We can explain why we can use more grammar. We can write like this.
public Date2(){ year = 1970; month = 1; day = 1; }
It is OK to write in this way, but the code is redundant, so it is not suitable.
We can write like this.
public Date2(){ new Date2(1970,1,1); }
This means that we have created another object, which is not good, so we do not use this method.
4. Let's summarize:
Where this can be used:
(1) it can be used in the instance method to represent the current object [syntax format: this.]
(2) it can be used in the construction method to call other construction methods through the current construction method [syntax format: this (argument);]
(3) this() syntax can only appear in the first line of the construction method
Source code:
D42_theSitustionOfNotOmitTihsKeyword.java
D42_ConstructerWithDefaultValue
Address:
https://github.com/ruigege66/Java/blob/masterD42_theSitustionOfNotOmitTihsKeyword.java
https://github.com/ruigege66/Java/blob/master/D42_ConstructerWithDefaultValue
2.CSDN: https://blog.csdn.net/weixin_
3. Blog Park: https://www.cnblogs.com/ruige0000/
4. Welcome to pay attention to wechat public number: Fourier transform, personal public number, which is only used for learning and communication. The background replies to "gift pack" to obtain big data learning materials.