本文共 3758 字,大约阅读时间需要 12 分钟。
java对象实例化的方式有以下几种:
1、使用new2、工厂模式3、反射4、clone()方法5、反序列化方式/**
*/
public class Book implements Cloneable, Serializable { private static final long serialVersionUID = 1L;private Integer serialNum; // 序列号
private String name; // 图书名称public Book() {
System.out.println("默认构造函数");}public Book(Integer serialNum, String name) {
System.out.println("带参数构造函数");this.serialNum = serialNum;this.name = name;}public int getSerialNum() {
return serialNum;}public void setSerialNum(int serialNum) { this.serialNum = serialNum;}public String getName() { return name;}public void setName(String name) { this.name = name;}@Override
public String toString() { if (name == null) { return "书籍属性还未填写";}return serialNum + " : " + name;
}
@Override
protected Object clone() throws CloneNotSupportedException { return super.clone();}}class BookFactory {
public static Book getInstance(Integer serialNum, String name) { return new Book(serialNum, name);}}public class Instantiation {
// 1.通过new实例化对象public static Book newMode() { System.out.println("============================================================"); System.out.println("通过new实例化对象"); Book book = new Book(1, "大道扬帆"); return book;}// 2.通过工厂方式实例化对象,其本质还是new,但将实例化的细节交给了工厂来处理,屏蔽了和业务代码无关的功能,一定程度上实现了和业务代码的解耦public static Book factoryMode() { System.out.println("============================================================"); System.out.println("通过工厂方式实例化对象"); return BookFactory.getInstance(2, "大道扬帆");}// 3.通过反射方式实例化对象@SuppressWarnings("rawtypes")public static Book reflectMode() throws Exception { System.out.println("============================================================"); System.out.println("通过反射方式实例化对象"); Class[] parameterTypes = new Class[] {Integer.class, String.class}; Constructor constructor = Book.class.getConstructor(parameterTypes); Book book = (Book) constructor.newInstance(3, "大道扬帆"); return book;}// 3-1.通过反射方式实例化对象还有一种方式,但本质上其实和上面的一样,但需要类本身有无参构造函数public static Book reflectAnotherMode() throws Exception { System.out.println("============================================================"); System.out.println("通过反射方式实例化对象的另外一种方式"); Book book = (Book) Class.forName("instantiation.Book").newInstance(); return book;}// 4.调用已存在对象的clone()方法实例化对象public static Book cloneMode(Book book) throws Exception { System.out.println("============================================================"); System.out.println("调用已存在对象的clone()方法实例化对象"); Book newBook = (Book) book.clone(); System.out.println("原对象和克隆对象是同一个对象吗?" + (book == newBook)); return newBook;}// 5.对象序列化后可以通过反序列化创建对象public static Book unSerializedMode() throws Exception { System.out.println("============================================================"); System.out.println("对象序列化后可以通过反序列化创建对象"); Book book = new Book(5, "大道扬帆"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("D:/book.obj"))); objectOutputStream.writeObject(book); ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("D:/book.obj"))); Book newBook = (Book) objectInputStream.readObject(); System.out.println("原对象和反序列化的对象是同一个吗?" + (book == newBook)); return newBook;}public static void main(String[] args) throws Exception { System.out.println(Instantiation.newMode()); System.out.println(Instantiation.factoryMode()); System.out.println(Instantiation.reflectMode()); System.out.println(Instantiation.reflectAnotherMode()); Book book = new Book(4, "大道扬帆"); System.out.println(Instantiation.cloneMode(book)); System.out.println(Instantiation.unSerializedMode());}
}
看精彩玄幻世界,尽在《大道扬帆》
转载于:https://blog.51cto.com/13855604/2170171