this的使用场景
-
1,成员变量和局部变量重名的时候,使用this区分要调用的是成员变量
-
2,构造方法中去调用另外一个构造方法 this() 必须出现在首行
public class Car { public String brand; public double price; public Car() { } public Car(String brand) { this.brand = brand; } public Car(String brand, double price) { //去调用另一个构造方法 this(brand); this.price = price; } public void run() { //String brand = "abc"; System.out.println(this.brand + "在跑"); } }
法中去调用另外一个构造方法 this() 必须出现在首行