- 相關(guān)推薦
Java中創(chuàng)建對(duì)象的方式
導(dǎo)語(yǔ):作為Java開發(fā)者,我們每天創(chuàng)建很多對(duì)象,下面是Java中創(chuàng)建對(duì)象的方式,一起來(lái)學(xué)習(xí)下吧:
1、使用new關(guān)鍵字
這是最常見也是最簡(jiǎn)單的創(chuàng)建對(duì)象的方式了。通過(guò)這種方式,我們可以調(diào)用任意的構(gòu)造函數(shù)(無(wú)參的和帶參數(shù)的)。
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方法創(chuàng)建對(duì)象。這個(gè)newInstance方法調(diào)用無(wú)參的構(gòu)造函數(shù)創(chuàng)建對(duì)象。
我們可以通過(guò)下面方式調(diào)用newInstance方法創(chuàng)建對(duì)象:
Employee emp2 = (Employee)
Class.forName("org.programming.mitra.exercises.Employee").newInstance();
或者
Employee emp2 = Employee.class.newInstance();
51: invokevirtual #70 // Method java/lang/Class.newInstance:()Ljava/lang/Object;
3、使用Constructor類的newInstance方法
和Class類的newInstance方法很像, java.lang.reflect.Constructor類里也有一個(gè)newInstance方法可以創(chuàng)建對(duì)象。我們可以通過(guò)這個(gè)newInstance方法調(diào)用有參數(shù)的和私有的構(gòu)造函數(shù)。
Constructor constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();
111: invokevirtual #80 // Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object;
newInstance方法內(nèi)部調(diào)用Constructor的newInstance方法。這也是眾多框架,如Spring、Hibernate、Struts等使用后者的原因。
4、使用clone方法
無(wú)論何時(shí)我們調(diào)用一個(gè)對(duì)象的clone方法,jvm就會(huì)創(chuàng)建一個(gè)新的對(duì)象,將前面對(duì)象的內(nèi)容全部拷貝進(jìn)去。用clone方法創(chuàng)建對(duì)象并不會(huì)調(diào)用任何構(gòu)造函數(shù)。
要使用clone方法,我們需要先實(shí)現(xiàn)Cloneable接口并實(shí)現(xiàn)其定義的clone方法。
Employee emp4 = (Employee) emp3.clone();’
162: invokevirtual #87 // Method org/programming/mitra/exercises/Employee.clone ()Ljava/lang/Object;
5、使用反序列化
當(dāng)我們序列化和反序列化一個(gè)對(duì)象,jvm會(huì)給我們創(chuàng)建一個(gè)單獨(dú)的對(duì)象。在反序列化時(shí),jvm創(chuàng)建對(duì)象并不會(huì)調(diào)用任何構(gòu)造函數(shù)。
為了反序列化一個(gè)對(duì)象,我們需要讓我們的類實(shí)現(xiàn)Serializable接口。
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();
261: invokevirtual #118
// Method java/io/ObjectInputStream.readObject:()Ljava/lang/Object;
我們從上面的字節(jié)碼片段可以看到,除了第1個(gè)方法,其他4個(gè)方法全都轉(zhuǎn)變?yōu)閕nvokevirtual(創(chuàng)建對(duì)象的直接方法),第一個(gè)方法轉(zhuǎn)變?yōu)閮蓚(gè)調(diào)用,new和invokespecial(構(gòu)造函數(shù)調(diào)用)。
例子
讓我們看一看為下面這個(gè)Employee類創(chuàng)建對(duì)象:
class Employee implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private String name;
public Employee() {
System.out.println("Employee Constructor Called...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Employee [name=" + name + "]";
}
@Override
public Object clone() {
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
}
下面的Java程序中,我們將用5種方式創(chuàng)建Employee對(duì)象。你可以從GitHub找到這些代碼。
public class ObjectCreation {
public static void main(String... args) throws Exception {
// By using new keyword
Employee emp1 = new Employee();
emp1.setName("Naresh");
System.out.println(emp1 + ", hashcode : " + emp1.hashCode());
// By using Class class's newInstance() method
Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee")
.newInstance();
// Or we can simply do this
// Employee emp2 = Employee.class.newInstance();
emp2.setName("Rishi");
System.out.println(emp2 + ", hashcode : " + emp2.hashCode());
// By using Constructor class's newInstance() method
Constructor constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();
emp3.setName("Yogesh");
System.out.println(emp3 + ", hashcode : " + emp3.hashCode());
// By using clone() method
Employee emp4 = (Employee) emp3.clone();
emp4.setName("Atul");
System.out.println(emp4 + ", hashcode : " + emp4.hashCode());
// By using Deserialization
// Serialization
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"));
out.writeObject(emp4);
out.close();
//Deserialization
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();
in.close();
emp5.setName("Akash");
System.out.println(emp5 + ", hashcode : " + emp5.hashCode());
}
}
程序會(huì)輸出:
public class ObjectCreation {
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中創(chuàng)建對(duì)象的方式】相關(guān)文章:
Java中創(chuàng)建對(duì)象的5種方法06-04
關(guān)于Java中對(duì)象的銷毀方法分析09-03
講解Java中如何構(gòu)造內(nèi)部類對(duì)象及訪問(wèn)對(duì)象07-24
Java中對(duì)象類型如何進(jìn)行轉(zhuǎn)換06-25
Java將對(duì)象保存到文件中再?gòu)奈募凶x取對(duì)象的方法推薦09-05
java面向?qū)ο缶幊讨v解06-18