Java對(duì)象創(chuàng)建的幾種方式總結(jié)
先上關(guān)鍵內(nèi)容,所用到的代碼請(qǐng)參考文末示例代碼。
一、使用new關(guān)鍵字創(chuàng)建對(duì)象
這是一種最常用的創(chuàng)建對(duì)象的方式。
Student student1 = new Student();
二、使用Class的newInstance()方法創(chuàng)建對(duì)象
需要有一個(gè)無(wú)參構(gòu)造方法,這個(gè)newInstance()方法調(diào)用無(wú)參的構(gòu)造函數(shù)創(chuàng)建對(duì)象。
類名.calss.newInstance( )
Student student2 = Student.class.newInstance();
該方法就是反射機(jī)制,事實(shí)上Class的newInstance()方法內(nèi)部就是調(diào)用Constructor的newInstance()方法。
Class類的newInstance只能觸發(fā)無(wú)參構(gòu)造方法創(chuàng)建對(duì)象,而構(gòu)造器類的newInstance能觸發(fā)有參數(shù)或者任意參數(shù)的構(gòu)造方法來(lái)創(chuàng)建對(duì)象。
三、使用Constructor類的newInstance()方法創(chuàng)建對(duì)象
java.lang.reflect.Constructor類里也有一個(gè)newInstance()方法可以創(chuàng)建對(duì)象。我們可以通過(guò)這個(gè)newInstance()方法調(diào)用有參數(shù)的和私有的構(gòu)造函數(shù)。
Constructor student3 = Constructor.class.newInstance();
五、使用反序列化創(chuàng)建對(duì)象
Java序列化是指把Java對(duì)象轉(zhuǎn)換為字節(jié)序列的過(guò)程,而Java反序列化是指把字節(jié)序列恢復(fù)為Java對(duì)象的過(guò)程;
使用反序列化:當(dāng)我們序列化和反序列化一個(gè)對(duì)象,jvm會(huì)給我們創(chuàng)建一個(gè)單獨(dú)的對(duì)象。在反序列化時(shí),jvm創(chuàng)建對(duì)象并不會(huì)調(diào)用任何構(gòu)造函數(shù)。
為了反序列化一個(gè)對(duì)象,我們需要讓我們的類實(shí)現(xiàn)Serializable接口。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME)); // 5、使用反序列化創(chuàng)建對(duì)象 Object student5 = ois.readObject();
六、創(chuàng)建對(duì)象的5種方式調(diào)用構(gòu)造器總結(jié)
創(chuàng)建對(duì)象的方式 | 是否調(diào)用了構(gòu)造器 |
使用new關(guān)鍵字創(chuàng)建對(duì)象 | 是 |
Class.newInstance() | 是 |
Constructor.newInstance() | 是 |
clone() | 否 |
反序列化 | 否 |
Java創(chuàng)建實(shí)例對(duì)象是不是必須要通過(guò)構(gòu)造函數(shù)?這其實(shí)是衍生出來(lái)的一個(gè)面試題。 上面問(wèn)題的答案很明顯了:Java創(chuàng)建實(shí)例對(duì)象,并不一定必須要調(diào)用構(gòu)造器的。
七、示例代碼(全)
以下是本文所用到的所有示例代碼。
7.1 編寫(xiě)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; @Override public Student clone() { try { Student clone = (Student) super.clone(); // TODO: copy mutable state here, so the clone can't change the internals of the original return clone; } catch (CloneNotSupportedException e) { throw new AssertionError(); } } }
7.2 編寫(xiě)測(cè)試類
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)建對(duì)象 Student student1 = new Student(); System.out.println("使用new關(guān)鍵字創(chuàng)建對(duì)象:" + student1); // 2、使用Class類的newInstance()方法創(chuàng)建對(duì)象 Student student2 = Student.class.newInstance(); System.out.println("使用Class類的newInstance()方法創(chuàng)建對(duì)象:" + student2); // 3、使用Constructor類的newInstance()方法創(chuàng)建對(duì)象 Constructor student3 = Constructor.class.newInstance(); System.out.println("使用Constructor類的newInstance()方法創(chuàng)建對(duì)象:" + student3); // 4、使用clone()方法創(chuàng)建對(duì)象 Student student4 = student2.clone(); System.out.println("使用clone()方法創(chuàng)建對(duì)象:" + student4); try { // Java序列化是指把Java對(duì)象轉(zhuǎn)換為字節(jié)序列的過(guò)程,而Java反序列化是指把字節(jié)序列恢復(fù)為Java對(duì)象的過(guò)程; // 序列化對(duì)象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME)); oos.writeObject(student1); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME)); // 5、使用反序列化創(chuàng)建對(duì)象 Object student5 = ois.readObject(); System.out.println("使用反序列化創(chuàng)建對(duì)象:" + student5); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } }
到此這篇關(guān)于Java對(duì)象創(chuàng)建的幾種方式總結(jié)的文章就介紹到這了,更多相關(guān)Java對(duì)象創(chuàng)建方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring的事件監(jiān)聽(tīng)機(jī)制示例詳解
這篇文章主要給大家介紹了關(guān)于Spring的事件監(jiān)聽(tīng)機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Spring中的FactoryBean與BeanFactory詳細(xì)解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細(xì)解析,在Spring框架中,FactoryBean和BeanFactory是兩個(gè)關(guān)鍵的接口,用于創(chuàng)建和管理對(duì)象實(shí)例,它們?cè)赟pring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11使用Spring AOP實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)讀寫(xiě)分離案例分析(附demo)
分布式環(huán)境下數(shù)據(jù)庫(kù)的讀寫(xiě)分離策略是解決數(shù)據(jù)庫(kù)讀寫(xiě)性能瓶頸的一個(gè)關(guān)鍵解決方案,這篇文章主要介紹了使用Spring AOP實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)讀寫(xiě)分離案例分析(附demo),有興趣的可以了解一下。2017-01-01Java ArrayList 實(shí)現(xiàn)實(shí)例講解
ArrayList是基于數(shù)組實(shí)現(xiàn)的,是一個(gè)動(dòng)態(tài)數(shù)組,其容量能自動(dòng)增長(zhǎng),類似于C語(yǔ)言中的動(dòng)態(tài)申請(qǐng)內(nèi)存,動(dòng)態(tài)增長(zhǎng)內(nèi)存。這篇文章主要介紹了java ArrayList 實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-11-11