Java this關(guān)鍵字的使用詳解
1. 先看一段代碼,并分析問題


public class This01 {
//編寫一個(gè)main方法
public static void main(String[] args) {
Dog dog1 = new Dog("大壯", 3);
//dog1調(diào)用了 info()方法
dog1.info();
}
}
class Dog{ //類
String name;
int age;
// public Dog(String dName, int dAge){//構(gòu)造器
// name = dName;
// age = dAge;
// }
//如果我們構(gòu)造器的形參,能夠直接寫成屬性名,就更好了
//但是出現(xiàn)了一個(gè)問題,根據(jù)變量的作用域原則
//構(gòu)造器的name 是局部變量,而不是屬性
//構(gòu)造器的age 是局部變量,而不是屬性
//==> 引出this關(guān)鍵字來解決
public Dog(String name, int age){//構(gòu)造器
//this.name 就是當(dāng)前對象的屬性name
this.name = name;
//this.age 就是當(dāng)前對象的屬性age
this.age = age;
}
public void info(){//成員方法,輸出屬性x信息
System.out.println(name + "\t" + age + "\t");
}
}

2. 深入理解 this
為了進(jìn)一步理解 this,我們再看一個(gè)案例 (This01.java)


使用hashCode()看看對象的情況

public class This01 {
//編寫一個(gè)main方法
public static void main(String[] args) {
Dog dog1 = new Dog("大壯", 3);
System.out.println("dog1的hashcode=" + dog1.hashCode());
//dog1調(diào)用了 info()方法
dog1.info();
System.out.println("============");
Dog dog2 = new Dog("大黃", 2);
System.out.println("dog2的hashcode=" + dog2.hashCode());
dog2.info();
}
}
class Dog{ //類
String name;
int age;
public Dog(String name, int age){//構(gòu)造器
//this.name 就是當(dāng)前對象的屬性name
this.name = name;
//this.age 就是當(dāng)前對象的屬性age
this.age = age;
System.out.println("this.hashCode=" + this.hashCode());
}
public void info(){//成員方法,輸出屬性x信息
System.out.println("this.hashCode=" + this.hashCode());
System.out.println(name + "\t" + age + "\t");
}
}

3. this 的注意事項(xiàng)和使用細(xì)節(jié)
ThisDetail.java
- this 關(guān)鍵字可以用來訪問本類的屬性、方法、構(gòu)造器
- this 用于區(qū)分當(dāng)前類的屬性和局部變量
public class ThisDetail {
public static void main(String[] args) {
T t = new T();
t.f3();
}
}
class T{
String name = "兮動(dòng)人";
int num = 10;
//this關(guān)鍵字可以用來訪問本類的屬性
public void f3(){
String name = "smith";
//傳統(tǒng)方式
System.out.println("name=" + name + " num=" + num);//smith 100
//也可以使用this訪問屬性
System.out.println("name=" + this.name + " num=" + this.num);//jack 100
}
}

訪問成員方法的語法:this.方法名(參數(shù)列表);
public class ThisDetail {
public static void main(String[] args) {
T t1 = new T();
t.f2();
}
}
class T {
public void f1(){
System.out.println("f1()方法...");
}
public void f2(){
System.out.println("f2()方法...");
//調(diào)用本類的 f1
//第一種方式
f1();
//第二種方式
this.f1();
}
}

訪問構(gòu)造器語法:this(參數(shù)列表); 注意只能在構(gòu)造器中使用(即只能在構(gòu)造器中訪問另外一個(gè)構(gòu)造器, 必須放在第一條語句)
public class ThisDetail {
public static void main(String[] args) {
T t2 = new T();
}
}
class T{
/*
細(xì)節(jié): 訪問構(gòu)造器語法:this(參數(shù)列表);
注意只能在構(gòu)造器中使用(即只能在構(gòu)造器中訪問另外一個(gè)構(gòu)造器)
注意: 訪問構(gòu)造器語法:this(參數(shù)列表); 必須放置第一條語句
*/
public T(){
//這里去訪問 T(String name,int age)構(gòu)造器,必須放在第一行
this("Jack", 23);
System.out.println("T()構(gòu)造器");
}
public T(String name,int age){
System.out.println("T(String name,int age)構(gòu)造器");
}
}

this 不能在類定義的外部使用,只能在類定義的方法中使用。
4. this 的案例
TestPerson.java
定義 Person 類,里面有 name、age 屬性,并提供 compareTo 比較方法,用于判斷是否和另一個(gè)人相等,提供測試類 TestPerson 用于測試, 名字和年齡完全一樣,就返回 true, 否則返回 false
public class TestPerson {
//編寫一個(gè)main方法
public static void main(String[] args) {
Person p1 = new Person("mary", 20);
Person p2 = new Person("mary", 20);
System.out.println("p1和p2比較的結(jié)果=" + p1.compareTo(p2));
}
}
/*
定義Person類,里面有name、age屬性,并提供compareTo比較方法,
用于判斷是否和另一個(gè)人相等,提供測試類TestPerson用于測試,
名字和年齡完全一樣,就返回true, 否則返回false
*/
class Person {
String name;
int age;
//構(gòu)造器
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//compareTo比較方法
public boolean compareTo(Person p) {
//名字和年齡完全一樣
// if(this.name.equals(p.name) && this.age == p.age) {
// return true;
// } else {
// return false;
// }
return this.name.equals(p.name) && this.age == p.age;
}
}

把名字或年齡改成其他不同數(shù)據(jù)

到此這篇關(guān)于Java this關(guān)鍵字的使用詳解的文章就介紹到這了,更多相關(guān)Java this內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java操作mongodb增刪改查的基本操作實(shí)戰(zhàn)指南
MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫,由c++語言編寫,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案,下面這篇文章主要給大家介紹了關(guān)于Java操作mongodb增刪改查的基本操作實(shí)戰(zhàn)指南,需要的朋友可以參考下2023-05-05
Java由淺入深細(xì)數(shù)數(shù)組的操作上
數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對數(shù)組的實(shí)現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲(chǔ)固定大小的同類型元素2022-04-04
maven創(chuàng)建spark項(xiàng)目的pom.xml文件配置demo
這篇文章主要為大家介紹了maven創(chuàng)建spark項(xiàng)目的pom.xml文件配置demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
淺談java對象結(jié)構(gòu) 對象頭 Markword
這篇文章主要介紹了淺談java對象結(jié)構(gòu) 對象頭 Markword,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

