天天看点

Java中5种创建对象的方式1、Java中的5种创建对象的方式2、User类3、创建方法

1、Java中的5种创建对象的方式

方法 数据库
使用new关键字 调用了构造函数
使用Class类的newInstance方法 调用了构造函数
使用Constructor类的newInstance方法 调用了构造函数
使用clone方法 没有调用构造函数
使用反序列化 没有调用构造函数

2、User类

需要使用克隆与序列化,因此需要实现Serializable、Cloneable接口。

public class User implements Serializable, Cloneable{
    private static final long serialVersionUID = -L;
    static {
        System.out.println("静态代码块");
    }

    private String name;
    private int age;

    public User(){
        System.out.println("无参构造");
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("有参构造");
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return name + " is: " + age + " years old.";
    }

    @Override
    protected User clone() throws CloneNotSupportedException {

        return (User) super.clone();
    }
}
           

3、创建方法

3.1 使用new关键字

该方法可以直接创建有参与无参对象。

User user = new User();
//User user = new User("11", );
user.setAge();
user.setName("11");
System.out.println(user.toString());
           

3.2 使用 Class 类的 newInstance 方法

使用 Class 类的 newInstance 方法创建对象,该方法调用无参构造函数创建对象。

User user = (User) Class.forName("com.object.User").newInstance();
//User user = User.class.newInstance();
user.setAge();
user.setName("22");
System.out.println(user.toString());
           

3.3 使用 Constructor 类的 newInstance 方法

//通过有参构造创建对象
Constructor<User> constructorWithArgument = User.class.getConstructor(new Class[]{String.class, int.class});
User userWithArgument = (User) constructorWithArgument.newInstance(new Object[]{"userWithArgument",});
System.out.println(userWithArgument.toString());
//通过无参构造创建对象
Constructor<User> constructorNoArgument = User.class.getConstructor();
User userNoArgument = constructorNoArgument.newInstance();
userNoArgument.setAge();
userNoArgument.setName("userNoArgument");
System.out.println(userNoArgument.toString());
           

3.4 使用 clone 方法

要使用 clone 方法,必须先实现 Cloneable 接口并实现并实现 clone 方法,用 clone 方法创建对象并不会调用任何构造函数。

User user = new User("11", );
User userClone = user.clone();
userClone.setName("clone");
System.out.println(userClone.toString());
           

3.5 使用反序列化

private static String serializeUser(User user) {
    ObjectOutputStream out = null;
    String path = "E:/user.obj";
    try {
        out = new ObjectOutputStream(new FileOutputStream(new File(path)));
        out.writeObject(user);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return path;
}

private static User deserializeUser(String path){
    ObjectInputStream in = null;
    User user = null;
    try {
        in = new ObjectInputStream(new FileInputStream(new File(path)));
        user = (User) in.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return user;
}

User userOld = new User("55",);
String path = serializeUser(userOld);
System.out.println(userOld.toString());
User userNew = deserializeUser(path);
System.out.println(userNew.toString());