创建Java对象的几种方式

使用new关键字

采用new关键字实例化对象是Java中最为常见的方法,下面是采用new关键字实例化对象的示例。在开始之前,我们先准备好一个Java类:SysUser.java。

目标类:

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @ClassName: SysUser
 * @Description: 用户
 * @author: xiaofei
 */
@Data
@AllArgsConstructor
public class SysUser {

    private Integer id;
    private String name;
}

接下来我们通过new关键字来创建几个用户:

public class TestSysUser {

    public static void main(String[] args) {
        // 第一种方式
        SysUser sysUser = new SysUser(1, "xiaofei");
    }
}

使用Class类的newInstance()方法

通过Class.forName()方法动态加载目标类,然后再调用newInstance()方法,动态实例化对象。

目标类:

@Data
public class SysUser {

    private Integer id = 2;
    private String name = "xiaofei";

}

通过newInstance()方法来创建新的对象:

public static void main(String[] args) {
        try {
            String sysUserName = "com.xffjs.demo.SysUser";
            Class clz = Class.forName(sysUserName);
            SysUser sysUser = (SysUser) clz.newInstance();
            System.out.println("id:" + sysUser.getId());
            System.out.println("name:" + sysUser.getName());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

运行main()方法,将输出如下信息:

id:2
name:xiaofei

使用Constructor.newInstance()方法

与Class类的newInstance()方法相似,我们还可以使用java.lang.reflect.Constructor类的newInstance()方法来创建新的对象。

目标类:

@Data
public class SysUser {

    private Integer id = 3;
    private String name = "xiaofei";

}

使用Constructor的newInstance()方法创建新的对象:

        Constructor<SysUser> sysUserConstructor;
        try {
            sysUserConstructor = SysUser.class.getConstructor();
            SysUser sysUser = sysUserConstructor.newInstance();
            System.out.println("id:" + sysUser.getId());
            System.out.println("name:" + sysUser.getName());
        }catch (Exception e){
            e.printStackTrace();
        }

使用反序列化

如果要使用反序列化创建对象,则目标类首先要实现Serializable接口。Serializable是一个标记接口。在本次示例中,我们先创建一个SysUser对象,并将其保存到data.txt文件中,然后在通过反序列化操作,读取data.txt中的数据,并创建新的对象。

目标类:

@Data
@AllArgsConstructor
public class SysUser implements Serializable {

    private Integer id;
    private String name;
}

示例:

        String filePath = "data.txt";
        SysUser sysUser = new SysUser(4, "反序列化");
        try {
            // 存入
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(sysUser);
            objectOutputStream.flush();
            objectOutputStream.close();

            // 取出
            FileInputStream fileInputStream = new FileInputStream(filePath);
            ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
            SysUser sysUser1 = (SysUser) inputStream.readObject();

            inputStream.close();

            System.out.println("id:" + sysUser1.getId());
            System.out.println("name:" + sysUser1.getName());
        }catch (Exception e){
            e.printStackTrace();
        }

使用对象克隆创建新的对象

clone()方法可以创建现有对象的副本,但在使用clone()方法前,需保证模板类实现了Cloneable接口,Cloneable接口也是一个标记类接口。

示例:

@Data
@AllArgsConstructor
public class SysUser implements Cloneable {

    private Integer id;
    private String name;

    public static void main(String[] args) {
        SysUser sysUser = new SysUser(5, "克隆");
        try {
            SysUser sysUser1 = (SysUser) sysUser.clone();
            System.out.println("id:" + sysUser1.getId());
            System.out.println("name:" + sysUser1.getName());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}