Java不同的方法创建对象的区别

走着路睡觉
  • java
小于 1 分钟

new

 MyObject object = new MyObject();

Class.forName()

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

clone()

MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();

object deserialization

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
上次编辑于:
贡献者: zhaojingbo
Loading...