Java中有几种创建对象的方式?

大概有这几种

  • new
  • Class 实例化
  • 构造实例化
  • 反序列化
  • 克隆
package jvm;

import model.Info;

import java.io.*;
import java.lang.reflect.InvocationTargetException;

public class ObjNewDemo {

    public static void main(String[] args) {
        // 1.new 的方式 调用构造
        {
            Info info = new Info("new Class");
            System.out.println("new Class ---> " + info.getInfo());
        }
        // 2.Class.newInstance 调用构造
        {
            try {
                Info info = Info.class.newInstance();
                System.out.println("Class.newInstance ---> " + info.getInfo());
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        // 3.Constructor.newInstance 调用构造
        {
            try {
                Info info = Info.class.getConstructor().newInstance();
                System.out.println("Constructor.newInstance ---> " + info.getInfo());
                Info infoWithParam = Info.class.getDeclaredConstructor(String.class).newInstance("Class.newInstance");
                System.out.println("Constructor.newInstance withParam ---> " + infoWithParam.getInfo());
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        // 4.反序列化
        {
            try {
                // 先序列化到文件
                ObjectOutputStream objOS = new ObjectOutputStream(new FileOutputStream("C://Info.tmp"));
                objOS.writeObject(new Info("Serializable"));
                objOS.flush();
                objOS.close();
                // 再通过反序列化创建对象
                ObjectInputStream objIS = new ObjectInputStream(new FileInputStream("C://Info.tmp"));
                Info info = (Info) objIS.readObject();
                System.out.println("Serializable ---> " + info.getInfo());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        // 5.克隆,被克隆的类必须实现 Cloneable 接口
        {
            Info srcObj = new Info("Cloneable");
            try {
                Info info = srcObj.clone();
                System.out.println("Cloneable ---> " + info.getInfo());
                System.out.println("Cloneable ---> (info == srcObj) ? " + (info == srcObj));
                System.out.println("Cloneable ---> (info.equals(srcObj)) ? " + info.equals(srcObj));
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

new Class ---> new Class
Class.newInstance ---> null
Constructor.newInstance ---> null
Constructor.newInstance withParam ---> Class.newInstance
Serializable ---> Serializable
Cloneable ---> Cloneable
Cloneable ---> (info == srcObj) ? false
Cloneable ---> (info.equals(srcObj)) ? true
posted @ 2024-01-03 18:24  艺言弈行  阅读(18)  评论(1编辑  收藏  举报