package com.lou.creation;
public class Client {
public static void main(String[] args) {
//1.使用new 方式创建对象
Worker worker1 = CreateInstanceUtil.createWorker();
Worker worker2 = CreateInstanceUtil.createWorker("Louis", 23);
//2.使用反射机制进行创建:
//a.使用反射机制,不带参数 Class 對象的 newInstance() 方法
Worker worker3 = CreateInstanceUtil.createWorker1();
//b.使用反射機制 , Constructor的 newInstance方法 不带参数
Worker worker4 = CreateInstanceUtil.createWorker2();
//c.使用反射机制 :带参数的构造函数创建新对象
Worker worker5 = CreateInstanceUtil.createWorker3("Louis", 23);
//3. 使用序列化和反序列化 进行创建,这个实际上是对象的复原
//将一个对象存入lou.dat文件内
CreateInstanceUtil.storeObject2File("lou.dat");
//将对象从文件取出
Worker worker6 = CreateInstanceUtil.createWorker4("lou.dat");
//4.利用对象的深层拷贝功能
Worker worker7 = CreateInstanceUtil.createWorker5(worker1);
}
}
package com.lou.creation;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class CreateInstanceUtil {
/*
* 方式1: 直接使用new的方式,不使用参数
*/
public static Worker createWorker() {
return new Worker();
}
/*
* 方式2: 使用new方式,带参数
*/
public static Worker createWorker(String name, int age) {
return new Worker(name, age);
}
/*
* 方式3: 使用反射机制,不带参数 Class 對象的 newInstance() 方法
*/
public static Worker createWorker1() {
Class clazz = null;
Worker worker = null;
try {
clazz = Class.forName("com.lou.creation.Worker");
worker = (Worker) clazz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return worker;
}
/*
* 方式4: 使用反射機制 , Constructor的 newInstance方法
*/
public static Worker createWorker2() {
Worker worker = null;
try {
Class clazz = null;
clazz = Class.forName("com.lou.creation.Worker");
// 获取不带参数的构造器
Constructor constructor = clazz.getConstructor();
// 使用构造器创建对象
worker = (Worker) constructor.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return worker;
}
/*
* 方式5: 使用反射机制 :带参数的构造函数创建新对象
*/
public static Worker createWorker3(String name, Integer age) {
Worker worker = null;
try {
Class clazz = null;
clazz = Class.forName("com.lou.creation.Worker");
// 获取不带参数的构造器
Constructor constructor = clazz.getConstructor(name.getClass(),
age.getClass());
// 使用构造器创建对象
worker = (Worker) constructor.newInstance(name, age);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return worker;
}
/*
* 方式6: 使用序列化和反序列化创建对象,这种方式其实是根据既有的对象进行复制,这个需要事先将可序列化的对象线存到文件里
*/
@SuppressWarnings("resource")
public static Worker createWorker4(String objectPath) {
ObjectInput input = null;
Worker worker = null;
try {
input = new ObjectInputStream(new FileInputStream(objectPath));
worker = (Worker) input.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return worker;
}
/*
* 将创建的对象存入到文件内
*/
public static void storeObject2File(String objectPath) {
Worker worker = new Worker();
ObjectOutputStream objectOutputStream;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(
objectPath));
objectOutputStream.writeObject(worker);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 方式7, 使用对象的 深复制进行复制,创建对象
*/
public static Worker createWorker5(Worker worker) {
return (Worker) worker.clone();
}
public static void main(String[] args) {
}
}
package com.lou.creation;
import java.io.Serializable;
public class Worker implements Cloneable,Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Worker()
{
this.name = "";
this.age = 0;
}
public Worker(String name,int age)
{
this.name = name;
this.age = age;
}
public void work()
{
System.out.println(name +"is working");
}
public Worker clone()
{
Worker worker = null;
try {
return (Worker) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return worker;
}
}