1. new
2. clone()
3. 通過反射機(jī)制創(chuàng)建
4. 序列化反序列化
反射示意代碼:// Class.forName方法獲取類,在調(diào)用類的newinstance()方法 Class cls = Class.forName("com.dao.User"); User u = (User)cls.newInstance();
序列化示意代碼:
//將一個對象實(shí)例化后,進(jìn)行序列化,再反序列化,也可以獲得一個對象(遠(yuǎn)程通信的場景下使用) ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream("D:/data.txt"));
//序列化對象 out.writeObject(user1); out.close();
//反序列化對象 ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:/data.txt")); User user2 = (User) in.readObject(); System.out.println("反序列化user:" + user2); in.close();