java基礎之 建立對象的幾種方式

來源:互聯網
上載者:User

標籤:

有4種顯式地建立對象的方式:

1.用new語句建立對象,這是最常用的建立對象的方式。

2.運用反射手段,調用java.lang.Class或者java.lang.reflect.Constructor類的newInstance()執行個體方法。

3.調用對象的clone()方法。

4.運用還原序列化手段,調用java.io.ObjectInputStream對象的readObject()方法.

下面示範了用前面3種方式建立對象的過程。

 

[java] view plaincopyprint?
  1. public class Customer implements Cloneable{  
  2. private String name;  
  3. private int age;  
  4. public Customer(){  
  5.   this("unknown",0);  
  6.   System.out.println("call default constructor");  
  7. }  
  8. public Customer(String name,int age){  
  9.   this.name=name;  
  10.   this.age=age;  
  11.   System.out.println("call second constructor");  
  12. }  
  13. public Object clone()throws CloneNotSupportedException{  
  14. return super.clone();  
  15. }  
  16. public boolean equals(Object o){  
  17.   if(this==o)return true;  
  18.   if(! (o instanceof Customer)) return false;  
  19.   final Customer other=(Customer)o;  
  20.   if(this.name.equals(other.name) && this.age==other.age)  
  21.      return true;  
  22.   else  
  23.      return false;  
  24. }  
  25. public String toString(){  
  26. return "name="+name+",age="+age;  
  27. }  
  28. public static void main(String args[])throws Exception{  
  29. //運用反射手段建立Customer對象  
  30. Class objClass=Class.forName("Customer");  
  31. Customer c1=(Customer)objClass.newInstance(); //會調用Customer類的預設構造方法  
  32. System.out.println("c1: "+c1); //列印name=unknown,age=0  
  33.    
  34. //用new語句建立Customer對象  
  35. Customer c2=new Customer("Tom",20);  
  36. System.out.println("c2: "+c2); //列印name=tom,age=20  
  37.    
  38. //運用複製手段建立Customer對象  
  39. Customer c3=(Customer)c2.clone(); //不會調用Customer類的構造方法  
  40. System.out.println("c2==c3 : "+(c2==c3)); //列印false  
  41. System.out.println("c2.equals(c3) : "+c2.equals(c3)); //列印true  
  42. System.out.println("c3: "+c3); //列印name=tom,age=20  
  43. }  
  44. }  
public class Customer implements Cloneable{private String name;private int age;public Customer(){  this("unknown",0);  System.out.println("call default constructor");}public Customer(String name,int age){  this.name=name;  this.age=age;  System.out.println("call second constructor");}public Object clone()throws CloneNotSupportedException{return super.clone();}public boolean equals(Object o){  if(this==o)return true;  if(! (o instanceof Customer)) return false;  final Customer other=(Customer)o;  if(this.name.equals(other.name) && this.age==other.age)     return true;  else     return false;}public String toString(){return "name="+name+",age="+age;}public static void main(String args[])throws Exception{//運用反射手段建立Customer對象Class objClass=Class.forName("Customer");Customer c1=(Customer)objClass.newInstance(); //會調用Customer類的預設構造方法System.out.println("c1: "+c1); //列印name=unknown,age=0 //用new語句建立Customer對象Customer c2=new Customer("Tom",20);System.out.println("c2: "+c2); //列印name=tom,age=20 //運用複製手段建立Customer對象Customer c3=(Customer)c2.clone(); //不會調用Customer類的構造方法System.out.println("c2==c3 : "+(c2==c3)); //列印falseSystem.out.println("c2.equals(c3) : "+c2.equals(c3)); //列印trueSystem.out.println("c3: "+c3); //列印name=tom,age=20}}

 

 

以上程式的列印結果如下:

call second constructor

call default constructor

c1: name=unknown,age=0

call second constructor

c2: name=Tom,age=20

c2==c3 : false

c2.equals(c3) : true

c3: name=Tom,age=20

從以上列印結果看出,用new語句或Class對象的newInstance()方法建立Customer對象時,都會執行Customer類的構造方法,而用對象的clone()方法建立Customer對象時,不會執行Customer類的構造方法。(區別)

除了以上4種顯式地建立對象的方式以外,在程式中還可以隱含地建立對象,包括以下幾種情況:

1.對於java命令中的每個命令列參數,Java虛擬機器都會建立相應的String對象,並把它們組織到一個String數組中,再把該數組作為參數傳給程式入口main(String args[])方法。

2.程式碼中的String類型的直接數對應一個String對象,例如:

 

[java] view plaincopyprint?
  1. String s1="Hello";  
  2. String s2="Hello"; //s2和s1引用同一個String對象  
  3. String s3=new String("Hello");  
  4. System.out.println(s1==s2); //列印true  
  5. System.out.println(s1==s3); //列印false  
String s1="Hello";String s2="Hello"; //s2和s1引用同一個String對象String s3=new String("Hello");System.out.println(s1==s2); //列印trueSystem.out.println(s1==s3); //列印false

 

執行完以上程式,記憶體中實際上只有兩個String對象,一個是直接數,由Java虛擬機器隱含地建立,還有一個通過new語句顯式地建立。

3.字串操作符“+”的運算結果為一個新的String對象。例如:

 

[java] view plaincopyprint?
  1. String s1="H";  
  2. String s2=" ello";  
  3. String s3=s1+s2; //s3引用一個新的String對象  
  4. System.out.println(s3=="Hello"); //列印false  
  5. System.out.println(s3.equals("Hello")); //列印true  
String s1="H";String s2=" ello";String s3=s1+s2; //s3引用一個新的String對象System.out.println(s3=="Hello"); //列印falseSystem.out.println(s3.equals("Hello")); //列印true

 

4.當Java虛擬機器載入一個類時,會隱含地建立描述這個類的Class執行個體.

java基礎之 建立對象的幾種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.