1、使用new关键字 } → 调用了构造函数
这种方式,我们可以调用任意的构造函数(无参的和带参数的)。
2、使用Class类的newInstance方法 } → 调用了构造函数
使用Class类的newInstance方法创建对象。这个newInstance方法调用无参的构造函数创建对象。
3、使用Constructor类的newInstance方法 } → 调用了构造函数
和Class类的newInstance方法很像, java.lang.reflect.Constructor类里也有一个newInstance方法可以创建对象。我们可以通过这个newInstance方法调用有参数的和私有的构造函数。
4、使用clone方法 } → 没有调用构造函数
无论何时我们调用一个对象的clone方法,jvm就会创建一个新的对象,将前面对象的内容全部拷贝进去。用clone方法创建对象并不会调用任何构造函数。
要使用clone方法,我们需要先实现Cloneable接口并实现其定义的clone方法。
5、使用反序列化 } → 没有调用构造函数
当我们序列化和反序列化一个对象,jvm会给我们创建一个单独的对象。在反序列化时,jvm创建对象并不会调用任何构造函数。
为了反序列化一个对象,我们需要让我们的类实现Serializable接口
代码demo
package javatest.demo; import java.io.Serializable; public class PrintTest implements Cloneable, Serializable { public static final long serivalVersion = 1L; private String name; public PrintTest() {
System.out.println("this is Constructor");
} public void hello() {
System.out.println("hello ");
} @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;
PrintTest other = (PrintTest) 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 "PrintTest [name=" + name + "]";
} @Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
测试类
package javatest.demo; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor; /**
* 创建一个类的几种方法?
*
* @author BrokenColor
* @date 2018年6月7日
*/
public class InstanceDemo { public static void main(String[] args) { // 使用new关键字} → 调用了构造函数
System.out.println("=================new关键字:");
PrintTest printtest1 = new PrintTest();
printtest1.setName("printtest1");
System.out.println(printtest1 + ",hashcode:" + printtest1.hashCode());
printtest1.hello();
try {
// 使用Class类的newInstance方法} → 调用了构造函数
System.out.println("=========Class类的newInstance方法:");
PrintTest printtest2 = (PrintTest) Class.forName("javatest.demo.PrintTest").newInstance();
//或者可以
// PringtTest pringttest2 = PringtTest.class.newInstance();
printtest2.setName("printtest2");
System.out.println(printtest2 + ",hashcode:" + printtest2.hashCode());
printtest2.hello(); // 使用Constructor类的newInstance方法 } → 调用了构造函数
System.out.println("=======Constructor类的newInstance方法:");
Constructor<PrintTest> constructor = PrintTest.class.getConstructor();
PrintTest printTest3 = (PrintTest) constructor.newInstance();
printTest3.setName("printTest3");
System.out.println(printTest3 + ",hashcode:" + printTest3.hashCode());
printTest3.hello(); // 使用clone方法 } → 没有调用构造函数
System.out.println("=======使用clone方法 } → 没有调用构造函数");
PrintTest printTest4 = (PrintTest) printTest3.clone();
printTest4.setName("printTest4");
System.out.println(printTest4+",hashcode:"+printTest4.hashCode()); //序列化
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"));
out.writeObject(printTest4);
out.close(); // 使用反序列化 } → 没有调用构造函数
System.out.println("===========使用反序列化");
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
PrintTest printTest5 = (PrintTest) in.readObject();
printTest5.setName("printTest5");
System.out.println(printTest5+",hashcode:"+printTest5.hashCode());
printTest5.hello(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
运行类
运行结果: