福建省建設(shè)工程職業(yè)注冊網(wǎng)站如何建造一個網(wǎng)站
先上關(guān)鍵內(nèi)容,所用到的代碼請參考文末示例代碼。
一、使用new關(guān)鍵字創(chuàng)建對象
這是一種最常用的創(chuàng)建對象的方式。
Student student1 = new Student();
二、使用Class的newInstance()方法創(chuàng)建對象
需要有一個無參構(gòu)造方法,這個newInstance()方法調(diào)用無參的構(gòu)造函數(shù)創(chuàng)建對象。
類名.calss.newInstance( )
Student student2 = Student.class.newInstance();
該方法就是反射機制,事實上Class的newInstance()方法內(nèi)部就是調(diào)用Constructor的newInstance()方法。
Class類的newInstance只能觸發(fā)無參構(gòu)造方法創(chuàng)建對象,而構(gòu)造器類的newInstance能觸發(fā)有參數(shù)或者任意參數(shù)的構(gòu)造方法來創(chuàng)建對象。
三、使用Constructor類的newInstance()方法創(chuàng)建對象
java.lang.reflect.Constructor類里也有一個newInstance()方法可以創(chuàng)建對象。我們可以通過這個newInstance()方法調(diào)用有參數(shù)的和私有的構(gòu)造函數(shù)。
Constructor student3 = Constructor.class.newInstance();
四、使用克隆clone()方法創(chuàng)建對象
Tips:要使用clone()方法,我們需要先實現(xiàn)Cloneable接口并實現(xiàn)其定義的clone()方法
無論何時我們調(diào)用一個對象的clone()方法,jvm就會創(chuàng)建一個新的對象,將前面對象的內(nèi)容全部拷貝進去。用clone()方法創(chuàng)建對象并不會調(diào)用任何構(gòu)造函數(shù)。
Student student4 = new Student().clone();
五、使用反序列化創(chuàng)建對象
Java序列化是指把Java對象轉(zhuǎn)換為字節(jié)序列的過程,而Java反序列化是指把字節(jié)序列恢復(fù)為Java對象的過程;
使用反序列化:當我們序列化和反序列化一個對象,jvm會給我們創(chuàng)建一個單獨的對象。在反序列化時,jvm創(chuàng)建對象并不會調(diào)用任何構(gòu)造函數(shù)。
為了反序列化一個對象,我們需要讓我們的類實現(xiàn)Serializable接口。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
// 5、使用反序列化創(chuàng)建對象
Object student5 = ois.readObject();
六、創(chuàng)建對象的5種方式調(diào)用構(gòu)造器總結(jié)
創(chuàng)建對象的方式 | 是否調(diào)用了構(gòu)造器 |
使用new關(guān)鍵字創(chuàng)建對象 | 是 |
Class.newInstance() | 是 |
Constructor.newInstance() | 是 |
clone() | 否 |
反序列化 | 否 |
Java創(chuàng)建實例對象是不是必須要通過構(gòu)造函數(shù)?這其實是衍生出來的一個面試題。 上面問題的答案很明顯了:Java創(chuàng)建實例對象,并不一定必須要調(diào)用構(gòu)造器的。
七、示例代碼(全)
以下是本文所用到的所有示例代碼。
7.1?編寫Student學(xué)生類
package com.effective.chapter2.other;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Cloneable, Serializable {private String name;private Integer age;@Overridepublic Student clone() {try {Student clone = (Student) super.clone();// TODO: copy mutable state here, so the clone can't change the internals of the originalreturn clone;} catch (CloneNotSupportedException e) {throw new AssertionError();}}
}
7.2?編寫測試類
package com.effective.chapter2.other;import java.io.*;
import java.lang.reflect.Constructor;public class CreateObjectTest {private static final String FILE_NAME = "student.obj";public static void main(String[] args) throws InstantiationException, IllegalAccessException, IOException {// 1、使用new關(guān)鍵字創(chuàng)建對象Student student1 = new Student();System.out.println("使用new關(guān)鍵字創(chuàng)建對象:" + student1);// 2、使用Class類的newInstance()方法創(chuàng)建對象Student student2 = Student.class.newInstance();System.out.println("使用Class類的newInstance()方法創(chuàng)建對象:" + student2);// 3、使用Constructor類的newInstance()方法創(chuàng)建對象Constructor student3 = Constructor.class.newInstance();System.out.println("使用Constructor類的newInstance()方法創(chuàng)建對象:" + student3);// 4、使用clone()方法創(chuàng)建對象Student student4 = student2.clone();System.out.println("使用clone()方法創(chuàng)建對象:" + student4);try {// Java序列化是指把Java對象轉(zhuǎn)換為字節(jié)序列的過程,而Java反序列化是指把字節(jié)序列恢復(fù)為Java對象的過程;// 序列化對象ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME));oos.writeObject(student1);ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));// 5、使用反序列化創(chuàng)建對象Object student5 = ois.readObject();System.out.println("使用反序列化創(chuàng)建對象:" + student5);} catch (ClassNotFoundException e) {throw new RuntimeException(e);}}
}
完結(jié)!