五種方法創(chuàng)建 Java 對象,你知道幾種呢?
點擊上方藍色“程序猿DD”,選擇“設(shè)為星標”
回復“資源”獲取獨家整理的學習資料!

Java開發(fā)人員,我們通常每天都會創(chuàng)建許多對象,但是我們總是使用新的或依賴管理系統(tǒng)(例如Spring)來創(chuàng)建這些對象。但是,有更多方法可以創(chuàng)建本文中將要研究的對象。Spring BeanFactory。1、使用新關(guān)鍵字
Employee?emp1?=?new?Employee();
2.使用Class類的newInstance()方法
Class類的newInstance()方法創(chuàng)建一個對象。此newInstance()方法調(diào)用no-arg構(gòu)造函數(shù)來創(chuàng)建對象。newInstance()通過以下方式創(chuàng)建對象:Employee?emp2?=?(Employee)?Class.forName("org.programming.mitra.exercises.Employee").newInstance();
Employee?emp2?=?Employee.class.newInstance();
3.使用構(gòu)造方法類的newInstance()方法
Class類的newInstance()方法類似,java.lang.reflect.Constructor類中有一個newInstance()方法可用于創(chuàng)建對象。我們還可以使用此newInstance()方法來調(diào)用參數(shù)化構(gòu)造函數(shù)和私有構(gòu)造函數(shù)。Constructor ?constructor?=?Employee.class.getConstructor();
Employee?emp3?=?constructor.newInstance();
newInstance()方法都被稱為創(chuàng)建對象的反射方式。實際上,Class類的newInstance()方法在內(nèi)部使用了Constructor類的newInstance()方法。這就是為什么最好使用后一種方法,并且也將其用于諸如Spring,Hibernate,Struts等不同框架的原因。要了解這兩種newInstance()方法之間的區(qū)別,請閱讀使用Example在Java中通過Reflection創(chuàng)建對象。4.使用clone()方法:
clone()時,JVM實際上都會為我們創(chuàng)建一個新對象,并將先前對象的所有內(nèi)容復制到其中。使用clone方法創(chuàng)建對象不會調(diào)用任何構(gòu)造函數(shù)。clone()方法,我們需要實現(xiàn)Cloneable并在其中定義clone()方法。Employee?emp4?=?(Employee)?emp3.clone();
5.使用反序列化
ObjectInputStream?in?=?new?ObjectInputStream(new?FileInputStream("data.obj"));
Employee?emp5?=?(Employee)?in.readObject();
Employee類:class?Employee?implements?Cloneable,?Serializable?{
????private?static?final?long?serialVersionUID?=?1L;
????private?String?name;
????public?Employee()?{?System.out.println("Employee?Constructor?Called...");?}
????public?String?getName()?{?return?name;?}
????public?void?setName(String?name)?{?this.name?=?name;?}
????@Override
????public?boolean?equals(Object?o)?{
????????if?(this?==?o)?return?true;
????????if?(o?==?null?||?getClass()?!=?o.getClass())?return?false;
????????Employee?employee?=?(Employee)?o;
????????return?Objects.equals(name,?employee.name);
????}
????@Override
????public?int?hashCode()?{?return?Objects.hash(name);?}
????@Override
????public?String?toString()?{?return?String.format("Employee{name='%s'}",?name);?}
????@Override
????public?Object?clone()?{
????????Object?obj?=?null;
????????try?{
????????????obj?=?super.clone();
????????}?catch?(CloneNotSupportedException?e)?{
????????????e.printStackTrace();
????????}
????????return?obj;
????}
}
public?class?ObjectCreation?{
????public?static?void?main(String...?args)?throws?Exception?{
????????//?1.?Using?new?keyword
????????Employee?emp1?=?new?Employee();
????????emp1.setName("emp1");
????????//?2.?Using?Class?class's?newInstance()?method
????????Employee?emp2?=?Employee.class.newInstance();
????????emp2.setName("emp2");
????????//?3.?Using?Constructor?class's?newInstance()?method
????????Constructor?constructor?=?Employee.class.getConstructor();
????????Employee?emp3?=?constructor.newInstance();
????????emp3.setName("emp3");
????????//?4.?Using?clone()?method
????????Employee?emp4?=?(Employee)?emp3.clone();
????????emp4.setName("emp4");
????????//?Serialization
????????try?(ObjectOutputStream?out?=?new?ObjectOutputStream(new?FileOutputStream("data.obj")))?{
????????????out.writeObject(emp4);
????????}
????????//?5.?Using?Deserialization
????????Employee?emp5;
????????try?(ObjectInputStream?in?=?new?ObjectInputStream(new?FileInputStream("data.obj")))?{
????????????emp5?=?(Employee)?in.readObject();
????????????emp5.setName("emp5");
????????}
????????System.out.println(emp1?+?",?hashcode?:?"?+?emp1.hashCode());
????????System.out.println(emp2?+?",?hashcode?:?"?+?emp2.hashCode());
????????System.out.println(emp3?+?",?hashcode?:?"?+?emp3.hashCode());
????????System.out.println(emp4?+?",?hashcode?:?"?+?emp4.hashCode());
????????System.out.println(emp5?+?",?hashcode?:?"?+?emp5.hashCode());
????}
}
Employee?Constructor?Called...
Employee?Constructor?Called...
Employee?Constructor?Called...
Employee{name='emp1'},?hashcode?:?3117192
Employee{name='emp2'},?hashcode?:?3117193
Employee{name='emp3'},?hashcode?:?3117194
Employee{name='emp4'},?hashcode?:?3117195
Employee{name='emp5'},?hashcode?:?3117196
往期推薦
掃一掃,關(guān)注我
一起學習,一起進步
每周贈書,福利不斷
﹀
﹀
﹀
深度內(nèi)容
推薦加入
最近熱門內(nèi)容回顧? ?#技術(shù)人系列

評論
圖片
表情
