糯米文學吧

位置:首頁 > 計算機 > java語言

Java中創建對象的5種方法

java語言2.67W

作為Java開發者,我們每天都會創建大量的對象,但是,我們總是使用管理依賴系統(如Spring框架)來創建這些對象。其實還有其他方法可以創建對象,那麼下文將為大家進行詳細介紹,歡迎閲讀學習。

Java中創建對象的5種方法
  1.使用new關鍵字

這是最常見的創建對象的方法,並且也非常簡單。通過使用這種方法我們可以調用任何我們需要調用的構造函數。

Employee emp1 = new Employee();

0: new #19 // class org/programming/mitra/exercises/Employee

3: dup

4: invokespecial #21 // Method org/programming/mitra/exercises/Employee."":()V

  2.使用class類的newInstance方法

我們也可以使用class類的newInstance()方法來創建對象。此newInstance()方法調用無參構造函數以創建對象。

我們可以通過newInstance() 用以下方式創建對象:

Employee emp2 = (Employee) ame("oyee")nstance();

或者

Employee emp2 = nstance();

51: invokevirtual #70 // Method java/lang/nstance:()Ljava/lang/Object;

  3.使用構造函數類的 newInstance方法

與使用class類的newInstance()方法相似,tructor類中有一個可以用來創建對象的newInstance()函數方法。通過使用這個newInstance()方法我們也可以調用參數化構造函數和私有構造函數。

Constructor constructor = onstructor();

Employee emp3 = nstance();

111: invokevirtual #80 // Method java/lang/reflect/nstance:([Ljava/lang/Object;)Ljava/lang/Object;

這些 newInstance() 方法被認為是創建對象的反射手段。實際上,內部類的nstance()方法使用構造函數類的 newInstance() 方法。這就是為什麼後者是首選並且使用不同的框架如Spring, Hibernate, Struts等。

  4.使用clone方法

實際上無論何時我們調用clone() 方法,JAVA虛擬機都為我們創建了一個新的對象並且複製了之前對象的內容到這個新的對象中。使用 clone()方法創建對象不會調用任何構造函數。

為了在對象中使用clone()方法,我們需要在其中實現可克隆類型並定義clone()方法。

Employee emp4 = (Employee) e();

162: invokevirtual #87 // Method org/programming/mitra/exercises/e ()Ljava/lang/Object;

  5.使用反序列化

無論何時我們對一個對象進行序列化和反序列化,JAVA虛擬機都會為我們創建一個單獨的對象。在反序列化中,JAVA虛擬機不會使用任何構造函數來創建對象。

對一個對象進行序列化需要我們在類中實現可序列化的接口。

ObjectInputStream in = new ObjectInputStream(new FileInputStream(""));

Employee emp5 = (Employee) Object();

261: invokevirtual #118 // Method java/io/Object:()Ljava/lang/Object;

正如我們在以上的字節代碼片段中所看到的,除第一種被轉換為一個新的函數和一個 invokespecial 指令以外,其它4種方法都被調用並轉換為invokevirtual。

  示例

讓我們來看看準備創建對象的 Employee 類:

class Employee implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;

private String name;

public Employee() {

tln("Employee Constructor Called...");

}

public String getName() {

return name;

}

public void setName(String name) {

= name;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((name == null) ? 0 : Code());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != lass())

return false;

Employee other = (Employee) obj;

if (name == null) {

if ( != null)

return false;

} else if (!ls())

return false;

return true;

}

@Override

public String toString() {

return "Employee [name=" + name + "]";

}

@Override

public Object clone() {

Object obj = null;

try {

obj = e();

} catch (CloneNotSupportedException e) {

tStackTrace();

}

return obj;

}

}

在下面的Java程序中我們用5種方式來創建 Employee對象。

public class ObjectCreation {

public static void main(String... args) throws Exception {

// By using new keyword

Employee emp1 = new Employee();

ame("Naresh");

tln(emp1 + ", hashcode : " + Code());

// By using Class class's newInstance() method

Employee emp2 = (Employee) ame("oyee")

nstance();

// Or we can simply do this

// Employee emp2 = nstance();

ame("Rishi");

tln(emp2 + ", hashcode : " + Code());

// By using Constructor class's newInstance() method

Constructor constructor = onstructor();

Employee emp3 = nstance();

ame("Yogesh");

tln(emp3 + ", hashcode : " + Code());

// By using clone() method

Employee emp4 = (Employee) e();

ame("Atul");

tln(emp4 + ", hashcode : " + Code());

// By using Deserialization

// Serialization

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(""));

eObject(emp4);

e();

//Deserialization

ObjectInputStream in = new ObjectInputStream(new FileInputStream(""));

Employee emp5 = (Employee) Object();

e();

ame("Akash");

tln(emp5 + ", hashcode : " + Code());

}

}

  此程序輸出結果如下:

Employee Constructor Called...

Employee [name=Naresh], hashcode : -1968815046

Employee Constructor Called...

Employee [name=Rishi], hashcode : 78970652

Employee Constructor Called...

Employee [name=Yogesh], hashcode : -1641292792

Employee [name=Atul], hashcode : 2051657

Employee [name=Akash], hashcode : 63313419

標籤:JAVA 創建對象