javaee论坛

普通会员

225648

帖子

337

回复

351

积分

楼主
发表于 2017-06-09 18:17:09 | 查看: 679 | 回复: 0

Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码 
图片描述
如果你运行了末尾的的程序,你会发现方法1,2,3用构造函数创建对象,方法4,5没有调用构造函数。

1.使用new关键字

这是最常见也是最简单的创建对象的方式了。通过这种方式,我们可以调用任意的构造函数(无参的和带参数的)。

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">Employee emp1 = new Employee();< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">0: new #19 // class org/programming/mitra/exercises/Employee3: dup4: invokespecial #21 // Method org/programming/mitra/exercises/Employee."":()V

2.使用Class类的newInstance方法

我们也可以使用Class类的newInstance方法创建对象。这个newInstance方法调用无参的构造函数创建对象。

我们可以通过下面方式调用newInstance方法创建对象:

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance();

或者

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">Employee emp2 = Employee.class.newInstance();< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">51: invokevirtual #70 // Method java/lang/Class.newInstance:()Ljava/lang/Object;

3.使用Constructor类的newInstance方法

和Class类的newInstance方法很像, java.lang.reflect.Constructor类里也有一个newInstance方法可以创建对象。我们可以通过这个newInstance方法调用有参数的和私有的构造函数。

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">Constructor<Employee> constructor = Employee.class.getConstructor();Employee emp3 = constructor.newInstance();< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">111: invokevirtual #80 // Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object;

这两种newInstance方法就是大家所说的反射。事实上Class的newInstance方法内部调用Constructor的newInstance方法。这也是众多框架,如Spring、Hibernate、Struts等使用后者的原因。想了解这两个newInstance方法的区别,请看这篇Creating objects through Reflection in Java with Example.

4.使用clone方法

无论何时我们调用一个对象的clone方法,jvm就会创建一个新的对象,将前面对象的内容全部拷贝进去。用clone方法创建对象并不会调用任何构造函数。

要使用clone方法,我们需要先实现Cloneable接口并实现其定义的clone方法。

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">Employee emp4 = (Employee) emp3.clone();< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">162: invokevirtual #87 // Method org/programming/mitra/exercises/Employee.clone ()Ljava/lang/Object;

5.使用反序列化

当我们序列化和反序列化一个对象,jvm会给我们创建一个单独的对象。在反序列化时,jvm创建对象并不会调用任何构造函数。 
为了反序列化一个对象,我们需要让我们的类实现Serializable接口

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));Employee emp5 = (Employee) in.readObject();< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">261: invokevirtual #118 // Method java/io/ObjectInputStream.readObject:()Ljava/lang/Object;

我们从上面的字节码片段可以看到,除了第1个方法,其他4个方法全都转变为invokevirtual(创建对象的直接方法),第一个方法转变为两个调用,new和invokespecial(构造函数调用)。

例子

让我们看一看为下面这个Employee类创建对象:

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">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种方式创建Employee对象。你可以从GitHub找到这些代码。

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">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()); }}

程序会输出:

< class="ttyprint" style="overflow:auto; font-family:"Source Code Pro",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.45; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(248,249,250); text-align:justify">Employee Constructor Called...Employee [name=Naresh], hashcode : -1968815046Employee Constructor Called...Employee [name=Rishi], hashcode : 78970652Employee Constructor Called...Employee [name=Yogesh], hashcode : -1641292792Employee [name=Atul], hashcode : 2051657Employee [name=Akash], hashcode : 63313419

您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017