作為Java開發(fā)者,我們每天創(chuàng)建很多對(duì)象,但我們通常使用依賴管理系統(tǒng),比如Spring去創(chuàng)建對(duì)象。然而這里有很多創(chuàng)建對(duì)象的方法,我們會(huì)在這篇文章中學(xué)到。
Java中有5種創(chuàng)建對(duì)象的方式,下面給出它們的例子還有它們的字節(jié)碼
使用new關(guān)鍵字 | } → 調(diào)用了構(gòu)造函數(shù) |
使用Class類的newInstance方法 | } → 調(diào)用了構(gòu)造函數(shù) |
使用Constructor類的newInstance方法 | } → 調(diào)用了構(gòu)造函數(shù) |
使用clone方法 | } → 沒有調(diào)用構(gòu)造函數(shù) |
使用反序列化 | } → 沒有調(diào)用構(gòu)造函數(shù) |
作為Java開發(fā)者,我們每天創(chuàng)建很多對(duì)象,但我們通常使用依賴管理系統(tǒng),比如Spring去創(chuàng)建對(duì)象。然而這里有很多創(chuàng)建對(duì)象的方法,我們會(huì)在這篇文章中學(xué)到。
Java中有5種創(chuàng)建對(duì)象的方式,下面給出它們的例子還有它們的字節(jié)碼
如果你運(yùn)行了末尾的的程序,你會(huì)發(fā)現(xiàn)方法1,2,3用構(gòu)造函數(shù)創(chuàng)建對(duì)象,方法4,5沒有調(diào)用構(gòu)造函數(shù)。
1.使用new關(guān)鍵字
這是最常見也是最簡單的創(chuàng)建對(duì)象的方式了。通過這種方式,我們可以調(diào)用任意的構(gòu)造函數(shù)(無參的和帶參數(shù)的)。
1
2
3
4
|
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)用無參的構(gòu)造函數(shù)創(chuàng)建對(duì)象。
我們可以通過下面方式調(diào)用newInstance方法創(chuàng)建對(duì)象:
1
2
3
4
5
|
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ì)象。我們可以通過這個(gè)newInstance方法調(diào)用有參數(shù)的和私有的構(gòu)造函數(shù)。
1
2
3
|
Constructor<Employee> constructor = Employee. class .getConstructor(); Employee emp3 = constructor.newInstance(); 111 : invokevirtual # 80 // Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object; |
這兩種newInstance方法就是大家所說的反射。事實(shí)上Class的newInstance方法內(nèi)部調(diào)用Constructor的newInstance方法。這也是眾多框架,如Spring、Hibernate、Struts等使用后者的原因。想了解這兩個(gè)newInstance方法的區(qū)別,
4.使用clone方法
無論何時(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方法。
1
2
|
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接口
1
2
3
|
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ì)象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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ì)象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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<Employee> 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ì)輸出:
1
2
3
4
5
6
7
8
|
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 |
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/wxd0108/p/5685817.html