java中幾種建立對象的方式

來源:互聯網
上載者:User
 

在java程式中,對象可以被顯式地或者隱式地建立.
下面說說四種顯式的建立對象的方式:
    ●  用new語句建立對象
    ●  運用反射手段,調用java.lang.Class 或者 java.lang.reflect.Constructor 類的newInstance()執行個體方法
    ●  調用對象的clone()方法
    ●  運用序列化手段,調用java.io.ObjectInputStream 對象的 readObject()方法.

例子

package dgut.ke.javatest;

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 "customer.name = "+name+" customer.age = "+age;
 }
  /**
   * @param args
   */
 public static void main(String[] args) throws Exception{
  // 運用反射手段建立Customer對象
  Class objectClass = Class.forName("dgut.ke.javatest.Customer");
  Customer c1 = (Customer) objectClass.newInstance();
  System.out.println("c1 --> " + c1);
  
  //用new建立Customer對象
  Customer c2 = new Customer("Tom",20);
  System.out.println("c2 --> " + c2);
  
  //運用複製手段建立Customer對象
  Customer c3 = (Customer) c2.clone();
  System.out.println("c3 --> " + c3);
  System.out.println("c2 == c3 " + (c2==c3));
  System.out.println("c2.equals(c3) " + c2.equals(c3));
 }

}

 

Class類的forName方法public static Class<?> forName(String className)
throws ClassNotFoundException
返回與帶有給定字串名的類或介面相關聯的 Class 對象。調用此方法等效於:
  Class.forName(className, true, currentLoader)

其中 currentLoader 表示此類的定義類載入器。
例如,以下程式碼片段返回 java.lang.Thread 類的運行時 Class 描述符。

   Class t = Class.forName("java.lang.Thread")

調用 forName("X") 將導致名為 X 的類被初始化。

 

參數:
className - 所需類的完全限定名。
返回:
具有指定名的類的 Class 對象。
拋出:
LinkageError - 如果連結失敗
ExceptionInInitializerError - 如果此方法所激發的初始化失敗
ClassNotFoundException - 如果找不到該類
Object類的clone()方法

protected Object clone()
throws CloneNotSupportedException
建立並返回此對象的一個副本。“副本”的準確含義可能依賴於對象的類。一般來說,對於任何對象 x,如果運算式:
x.clone() != x
是正確的,則運算式:
x.clone().getClass() == x.getClass()
將為 true,但這些不是絕對條件。一般情況下是:
x.clone().equals(x)
將為 true,但這不是絕對條件。
按照慣例,返回的對象應該通過調用 super.clone 獲得。如果一個類及其所有的超類(Object 除外)都遵守此約定,則 x.clone().getClass() == x.getClass()。

按照慣例,此方法返回的對象應該獨立於該對象(正被複製的對象)。要獲得此獨立性,在 super.clone 返回對象之前,有必要對該對象的一個或多個欄位進行修改。這通常意味著要複製包含正在被複製對象的內部“深層結構”的所有可變對象,並使用對副本的引用替換對這些對象的引用。如果一個類只包含基本欄位或對不變對象的引用,那麼通常不需要修改 super.clone 返回的對象中的欄位。

Object 類的 clone 方法執行特定的複製操作。首先,如果此對象的類不能實現介面 Cloneable,則會拋出 CloneNotSupportedException。注意:所有的數組都被視為實現介面 Cloneable。否則,此方法會建立此對象的類的一個新執行個體,並像通過分配那樣,嚴格使用此對象相應欄位的內容初始化該對象的所有欄位;這些欄位的內容沒有被自我複製。所以,此方法執行的是該對象的“淺層複製 (Shallow Copy)”,而不“深層複製”操作。

Object 類本身不實現介面 Cloneable,所以在類為 Object 的對象上調用 clone 方法將會導致在運行時拋出異常。

 

返回:
此執行個體的一個複製。
拋出:
CloneNotSupportedException - 如果對象的類不支援 Cloneable 介面,則重寫 clone 方法的子類也會拋出此異常,以指示無法複製某個執行個體。
另請參見:
Cloneable

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.