Java:this
package com.demo.day;
public class This01 {
public static void main(String[] args) {
Dog1 dog1 = new Dog1("耗子",3);
dog1.info();
}
}
class Dog1{
String name;
int age;
// public Dog1(String dName,int dAge){
// name = dName;
// age = dAge;
// }
//出現(xiàn)了一個問題,根據(jù)變量的作用域原則
//構(gòu)造器的name 就是局部變量,而不是屬性
//引出 ————> this 關鍵字
public Dog1(String name,int age){
this.name = name; //當前對象的屬性name = 局部變量name
this.age = age; //當前對象的屬性age = 局部變量age
}
//如果我們構(gòu)造器的形參,能夠直接寫成屬性名就更好了
public void info(){
System.out.println(name + "\t" + age + "\t");
}
}
什么是this
java虛擬機會給每個對象分配this,代表當前對象。
使用hashCode()方法來查看this是否是本類的引用;
實際上,由 Object 類定義的 hashCode 方法確實會針對不同的對象返回不同的整數(shù)。(這一般是通過將該對象的內(nèi)部地址轉(zhuǎn)換成一個整數(shù)來實現(xiàn)的,但是 JavaTM 編程語言不需要這種實現(xiàn)技巧。)
package com.demo.day;
public class This01 {
public static void main(String[] args) {
Dog1 dog1 = new Dog1("耗子",3);
Dog1 dog2 = new Dog1("三花",2);
//調(diào)用hashCode()方法
System.out.println("dog1的hashcode:" + dog1.hashCode());
System.out.println("dog2的hashcode:" + dog2.hashCode());
// dog1.info();
}
}
class Dog1{
String name;
int age;
// public Dog1(String dName,int dAge){
// name = dName;
// age = dAge;
// }
//出現(xiàn)了一個問題,根據(jù)變量的作用域原則
//構(gòu)造器的name 就是局部變量,而不是屬性
//引出 ————> this 關鍵字
public Dog1(String name,int age){
this.name = name; //當前對象的屬性name = 局部變量name
this.age = age; //當前對象的屬性age = 局部變量age
//調(diào)用hashCode()方法
System.out.println("this.hashcode = " + this.hashCode());
}
//如果我們構(gòu)造器的形參,能夠直接寫成屬性名就更好了
public void info(){
System.out.println(name + "\t" + age + "\t");
}
}
輸出結(jié)果:
this.hashcode = 1324119927
this.hashcode = 81628611
dog1的hashcode:1324119927
dog2的hashcode:81628611
this小結(jié)
簡單的說,哪個對象調(diào)用,this就代表哪個對象
this使用細節(jié)
- this關鍵字可以用來訪問本類的屬性、方法、構(gòu)造器
- this用于區(qū)分當前類的屬性和局部變量
- 訪問成員方法的語法:this.方法名(參數(shù)列表);
- 訪問構(gòu)造器語法:this(參數(shù)列表); 注意只能在構(gòu)造器中使用
- this不能在類定義的外部使用,只能在類定義的方法中使用
下面代碼將上述細節(jié)呈現(xiàn)
package com.demo.day;
public class thisDetail {
public static void main(String[] args) {
// T1 t1 = new T1();
// t1.f2();
T1 t2 = new T1();
t2.f3();
}
}
class T1{
String name = "Tom";
int num;
//細節(jié):訪問成員方法的語句:this.方法名(參數(shù)列表)
//注意:訪問構(gòu)造器語法:this(參數(shù)列表);必須放在構(gòu)造器的第一條語句
//對構(gòu)造器的訪問 注意只能在構(gòu)造器中使用,即不能成員方法訪問構(gòu)造器,只能構(gòu)造器互相訪問
public T1(){
//從這里去訪問 T1(String name,int age) 構(gòu)造器
this("jack",21);//對this的調(diào)用必須是構(gòu)造器中的第一個語句
System.out.println("T1() 構(gòu)造器");
}
public T1(String name,int age){
System.out.println("T1(String name,int age) 構(gòu)造器");
}
public void f1(){
System.out.println("f1() 方法..");
}
public void f2(){
System.out.println("f2() 方法..");
//調(diào)用本類的 f1
//第一種方式
f1();
//第二種方式
this.f1();
}
//this 關鍵字可以用來訪問本類的屬性
public void f3(){
String name = "smith";//變量作用域的就近原則會導致下面的輸出不同
//傳統(tǒng)方式
System.out.println("name=" + name + "\tnum=" + num);
//也可以使用this訪問屬性
System.out.println("name=" + this.name + "\tnum=" + this.num);
}
}
測試
package com.demo.day;
public class TestPerson {
public static void main(String[] args) {
Person3 p = new Person3("smith",21);
Person3 p2 = new Person3("tom",21);
System.out.println(p.compareTo(p2));
}
}
class Person3{
String name;
int age;
Person3(String name,int age){
this.name = name;
this.age = age;
}
public boolean compareTo(Person3 p){
// if (this.name.equals(p.name) && this.age == p.age){
// return true;
// }
return this.name.equals(p.name) && this.age == p.age;
}
}

浙公網(wǎng)安備 33010602011771號