java 实例化对象的几种姿势
生活随笔
收集整理的這篇文章主要介紹了
java 实例化对象的几种姿势
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
代碼里面有注釋:
import java.io.*; import java.lang.reflect.Constructor;public class Hello implements Cloneable,Serializable{private String str;public Hello(String str){this.str = str;}public Hello(){this.str = "class";}public static void main(String[] args) throws Exception{Hello hello = Hello.getInstanceByNew();System.out.println(hello.str);System.out.println("=============================");try{Hello instanceByClone = Hello.getInstanceByClone(hello);System.out.println(instanceByClone.str);System.out.println(hello == instanceByClone);}catch (Exception e){e.printStackTrace();}System.out.println("=============================");Hello instanceByClass = Hello.getInstanceByClass();System.out.println(instanceByClass.str);System.out.println("=============================");System.out.println("使用無參構造");Hello instanceByConstructor = Hello.getInstanceByConstructor();System.out.println(instanceByConstructor.str);System.out.println("使用有參構造");Hello instanceByConstructor2 = Hello.getInstanceByConstructor("有參構造");System.out.println(instanceByConstructor2.str);System.out.println("=============================");/*** 首先 設置一個序列化文件位置*/String path = "hello.obj";ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path));Hello xlh = new Hello("序列化");objectOutputStream.writeObject(xlh);objectOutputStream.close();/*** 反序列化拿到對象*/Hello instanceBySerializable = Hello.getInstanceBySerializable(path);System.out.println(instanceBySerializable.hashCode()+"******************");System.out.println(xlh.str);System.out.println(instanceBySerializable.str);System.out.println("結束");}/*** 使用 new*/public static Hello getInstanceByNew(){return new Hello("new");}/*** 使用 clone 使用此方法 class類需要繼承 cloneable 接口*/public static Hello getInstanceByClone(Hello hello) throws CloneNotSupportedException{Hello clone1 = (Hello) hello.clone();return clone1;}/*** 使用Class的 newInstance 這個newInstance方法調用無參的構造函數創建對象*/public static Hello getInstanceByClass() throws Exception{Hello hello = (Hello) Class.forName("Hello").newInstance();return hello;}/*** 使用 構造器 無參函數*/public static Hello getInstanceByConstructor() throws Exception{Constructor<Hello> constructor = Hello.class.getConstructor();Hello hello = constructor.newInstance();return hello;}/*** 使用 構造器 有參函數*/public static Hello getInstanceByConstructor(String str) throws Exception{Constructor<Hello> constructor = Hello.class.getConstructor(String.class);Hello hello = constructor.newInstance(str);return hello;}/*** 使用反序列化 獲取實例化對象 對象的類要繼承 Serializable 接口表示可以進行序列化*/public static Hello getInstanceBySerializable(String path) throws Exception{ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(path));Object o = objectInputStream.readObject();objectInputStream.close();System.out.println(o.hashCode()+"******************");return (Hello) o;} }?
轉載于:https://my.oschina.net/viakiba/blog/1483852
總結
以上是生活随笔為你收集整理的java 实例化对象的几种姿势的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第十二单元文件的归档/压缩/传输
- 下一篇: Mockito对final类型和方法的支