亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

深入理解java中this關(guān)鍵字的使用

 更新時間:2017年08月14日 12:08:15   作者:美好的明天  
這篇文章主要介紹了this關(guān)鍵字的使用,通過調(diào)用構(gòu)造方法,使用this關(guān)鍵字調(diào)用當前對象等詳細介紹了this的特點和使用,需要的朋友可以參考下

一,表示類中屬性

1,沒有使用this的情況

class Person{  // 定義Person類
 private String name ;  // 姓名
 private int age ;   // 年齡
 public Person(String name,int age){ // 通過構(gòu)造方法賦值
  name = name ;
  age = age ;
 }
 public String getInfo(){ // 取得信息的方法
  return "姓名:" + name + ",年齡:" + age ;
 }
};
public class ThisDemo01{
 public static void main(String args[]){
  Person per1 = new Person("張三",33) ; // 調(diào)用構(gòu)造實例化對象
  System.out.println(per1.getInfo()) ; // 取得信息
 }
};

運行結(jié)果:

姓名:null,年齡:0;

可以得出結(jié)論:此時并沒有正確將內(nèi)容賦給屬性;

假設(shè)身邊有一只筆,遠處也有一只筆,肯定會就近拿身邊的筆。這在構(gòu)造方法中一樣。
所以此時操作的name,age都是構(gòu)造方法中定義的name,age.跟類中屬性完全不沾邊。
此時,為了明確哪個是類中的屬性,需要加上this.類中屬性。

class Person{  // 定義Person類
 private String name ;  // 姓名
 private int age ;   // 年齡
 public Person(String name,int age){ // 通過構(gòu)造方法賦值
  this.name = name ; // 為類中的name屬性賦值
  this.age = age ;// 為類中的age屬性賦值
 }
 public String getInfo(){ // 取得信息的方法
  return "姓名:" + name + ",年齡:" + age ;
 }
};
public class ThisDemo02{
 public static void main(String args[]){
  Person per1 = new Person("張三",33) ; // 調(diào)用構(gòu)造實例化對象
  System.out.println(per1.getInfo()) ; // 取得信息
 }
};

運行結(jié)果:

姓名:張三,年齡:33

二,this調(diào)用構(gòu)造方法

如果在類中有多個構(gòu)造方法,也可以利用this關(guān)鍵字互相調(diào)用。
假設(shè)一個類中存在多個構(gòu)造方法,但無論多少構(gòu)造方法,都要打造一個“新對象實例化”,此時就有兩種做法。

按照最原始方法:

class Person{  // 定義Person類
 private String name ;  // 姓名
 private int age ;   // 年齡
 public Person(){ // 無參構(gòu)造
  System.out.println("新對象實例化") ;
 }
 public Person(String name){
  System.out.println("新對象實例化") ;
  this.name = name ;
 }
 public Person(String name,int age){ // 通過構(gòu)造方法賦值
  System.out.println("新對象實例化") ;
  this.name = name ; // 為類中的name屬性賦值
  this.age = age ;// 為類中的age屬性賦值
 }
 public String getInfo(){ // 取得信息的方法
  return "姓名:" + name + ",年齡:" + age ;
 }
};
public class ThisDemo03{
 public static void main(String args[]){
  Person per1 = new Person("張三",33) ; // 調(diào)用構(gòu)造實例化對象
  System.out.println(per1.getInfo()) ; // 取得信息
 }
};

因為以上紅色部分重復(fù)了,現(xiàn)在只是一行,感覺不出來,但是如果現(xiàn)在代碼很多行的話,缺陷立刻顯現(xiàn)出來了。

此時,最好讓構(gòu)造方法間進行互相調(diào)用。

使用:this(若干參數(shù))的形式完成。

package methoud;
class Person{  // 定義Person類
 private String name ;  // 姓名
 private int age ;   // 年齡
 public Person(){ // 無參構(gòu)造  
  System.out.println("新對象實例化") ;
 }
 public Person(String name){
  this() ;// 調(diào)用本類中的無參構(gòu)造方法
  this.name = name ;
 }
 public Person(String name,int age){ // 通過構(gòu)造方法賦值
  this(name) ;// 調(diào)用有一個參數(shù)的構(gòu)造方法
  this.age = age ;// 為類中的age屬性賦值
 }
 public String getInfo(){ // 取得信息的方法
  return "姓名:" + name + ",年齡:" + age ;
 }
};
public class ThisDemo06{
 public static void main(String args[]){
  Person per1 = new Person("張三",33) ; // 調(diào)用構(gòu)造實例化對象
  System.out.println(per1.getInfo()) ; // 取得信息
 }
};

運行結(jié)果:

新對象實例化
姓名:張三,年齡:33

注意點:

在使用this關(guān)鍵字調(diào)用其他關(guān)鍵字的時候,有以下限制:

1)this()調(diào)用其他構(gòu)造方法的語句只能放在構(gòu)造方法(在其他普通方法里是不行的)的首行;

2)在使用this調(diào)用其他構(gòu)造方法的時候,至少有一個構(gòu)造方法是不用this調(diào)用的。(必須要有結(jié)尾,不能無限期的調(diào)用下去,循環(huán)遞歸調(diào)用);

如下就會出錯:

package methoud;
class Person{  // 定義Person類
 private String name ;  // 姓名
 private int age ;   // 年齡
 public Person(){ // 無參構(gòu)造
  System.out.println("新對象實例化") ;
 }
 public Person(String name){
  this.name = name ;
  this() ;// 調(diào)用this()方法只能放在構(gòu)造方法首行
 }
 public Person(String name,int age){ // 通過構(gòu)造方法賦值
  this(name) ;// 調(diào)用有一個參數(shù)的構(gòu)造方法
  this.age = age ;// 為類中的age屬性賦值
 }
 public String getInfo(){ // 取得信息的方法
  this() ;// 其他普通方法不能調(diào)用this()方法
  return "姓名:" + name + ",年齡:" + age ;
 }
};
public class ThisDemo04{
 public static void main(String args[]){
  Person per1 = new Person("張三",33) ; // 調(diào)用構(gòu)造實例化對象
  System.out.println(per1.getInfo()) ; // 取得信息
 }
};

三,使用this關(guān)鍵字調(diào)用當前對象。

當前對象:當前正在調(diào)用方法的對象。

如下,分別用兩種方法打印對象per1和per2

class Person{  // 定義Person類
 public String getInfo(){ // 取得信息的方法
  System.out.println("Person類 --> " + this) ; // 直接打印this
  return null ; // 為了保證語法正確,返回null
 }
};
public class ThisDemo06{
 public static void main(String args[]){
  Person per1 = new Person() ; // 調(diào)用構(gòu)造實例化對象
  Person per2 = new Person() ; // 調(diào)用構(gòu)造實例化對象
  System.out.println("MAIN方法 --> " + per1) ; // 直接打印對象
  per1.getInfo() ; // 當前調(diào)用getInfo()方法的對象是per1
  System.out.println("MAIN方法 --> " + per2) ; // 直接打印對象
  per2.getInfo() ; // 當前調(diào)用getInfo()方法的對象是per2
 }
};

運行結(jié)果:

MAIN方法 --> methoud.Person@2a139a55
Person類 --> methoud.Person@2a139a55
MAIN方法 --> methoud.Person@15db9742
Person類 --> methoud.Person@15db9742

可見,用this調(diào)用的是當前對象,與直接per1,per2是一樣的效果。

四,對象的比較

以下這個例子中,生成兩個對象,當對象中年齡和姓名完全相等的時候,則認為兩個對象是相等的,此時有兩個問題

1)如何進行對象比較:

2)在那塊對象比較:

String本身是一個類,如果要進行相等比較,需要使用equls(),而,age是Int,直接使用==判斷。

class Person{  // 定義Person類
 private String name ; // 姓名
 private int age ;  // 年齡
 public Person(String name,int age){
  this.setName(name) ;
  this.setAge(age) ;
 }
 public void setName(String name){ // 設(shè)置姓名
  this.name = name ;
 }
 public void setAge(int age){  // 設(shè)置年齡
  this.age = age ;
 }
 public String getName(){
  return this.name ;
 }
 public int getAge(){
  return this.age ;
 }
};
public class ThisDemo07{
 public static void main(String args[]){
  Person per1 = new Person("張三",30) ; // 聲明兩個對象,內(nèi)容完全相等
  Person per2 = new Person("張三",30) ; // 聲明兩個對象,內(nèi)容完全相等
  // 直接在主方法中依次取得各個屬性進行比較
  if(per1.getName().equals(per2.getName())&&per1.getAge()==per2.getAge()){
   System.out.println("兩個對象相等!") ;
  }else{
   System.out.println("兩個對象不相等!") ;
  }
 }
};

運行結(jié)果:

兩個對象相等!

以上代碼,功能確實實現(xiàn)了,但是由于代碼暴露在外面,不安全,容易出錯。
應(yīng)該由自己進行比較最合適,所以應(yīng)該在Person類中增加一個比較的方法。
當前對象調(diào)用傳人的對象,當前對象即調(diào)用方法的對象,用this表示,
這里表示在哪里比較。

如下:

class Person{  // 定義Person類
 private String name ; // 姓名
 private int age ;  // 年齡
 public Person(String name,int age){
  this.setName(name) ;
  this.setAge(age) ;
 }
 public boolean compare(Person per){
  // 調(diào)用此方法時里面存在兩個對象:當前對象、傳入的對象
   Person p1 = this ; // 當前的對象,就表示per1
  Person p2 = per ; // 傳遞進來的對象,就表示per2
  if(p1==p2){ // 判斷是不是同一個對象,用地址比較
   return true ;
  }
  // 之后分別判斷每一個屬性是否相等
  if(p1.name.equals(p2.name)&&p1.age==p2.age){
   return true ; // 兩個對象相等
  }else{
   return false ; // 兩個對象不相等
  }
 }
 public void setName(String name){ // 設(shè)置姓名
  this.name = name ;
 }
 public void setAge(int age){  // 設(shè)置年齡
  this.age = age ;
 }
 public String getName(){
  return this.name ;
 }
 public int getAge(){
  return this.age ;
 }
};
public class ThisDemo08{
 public static void main(String args[]){
  Person per1 = new Person("張三",30) ; // 聲明兩個對象,內(nèi)容完全相等
  Person per2 = new Person("張三",30) ; // 聲明兩個對象,內(nèi)容完全相等
  // 直接在主方法中依次取得各個屬性進行比較
  if(per1.compare(per2)){
   System.out.println("兩個對象相等!") ;
  }else{
   System.out.println("兩個對象不相等!") ;
  }
 }
};

運行結(jié)果:

兩個對象相等!

以上就是小編整理的全部內(nèi)容啦,希望大家繼續(xù)支持腳本之家~

相關(guān)文章

最新評論