オブジェクトを作成する6つの方法:
public class NewObject {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
NewObject newObject = new NewObject();
//1.反射构造器
Person person1 = newObject.construct();
System.out.println(person1);
//2.newInstance
Person person2 = newObject.classNewInstance();
System.out.println(person2);
//3.clone
Person person3 = newObject.byClone();
System.out.println(person3);
//4.序列化
Person serial = newObject.serial();
System.out.println(serial.getAge());
//5.unsafe
Person unsafe = newObject.unsafe();
System.out.println(unsafe);
//6.new
Person p=new Person();
}
/**
* 反射构造器构造
*
* @return
*/
public Person construct() {
Class aClass = null;
try {
aClass = Class.forName("cn.xys.atomic.unsafe.Person");
Constructor constructor = aClass.getConstructor(null);
Person person = (Person) constructor.newInstance(null);
return person;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
/**
* 2.newInstance
*
* @return
* @throws IllegalAccessException
* @throws InstantiationException
*/
public Person classNewInstance() throws IllegalAccessException, InstantiationException {
return Person.class.newInstance();
}
/**
* 克隆
*
* @return
*/
public Person byClone() {
try {
//需实现Cloneable接口,并重写clone()方法
Person person = new Person();
return (Person) person.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
/**
* 反序列化
* @return
*/
public Person serial() {
Person person = new Person();
person.setAge(1);
try (FileOutputStream fos = new FileOutputStream("D:\\d\\a.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(person);
FileInputStream fis = new FileInputStream("D:\\d\\a.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
return (Person) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* unsafe创建
* @return
*/
public Person unsafe(){
Field filed = null;
try {
//获取unsafe实例
filed = Unsafe.class.getDeclaredField("theUnsafe");
filed.setAccessible(true);
Unsafe unsafe = (Unsafe) filed.get(null);
//分配一个Person实例,此处只分配内存,不会调用构造函数
Person person = (Person) unsafe.allocateInstance(Person.class);
return person;
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
class Person implements Cloneable, Serializable {
private String name;
private int age;
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}