java 创建对象的几种方式

以下介绍5种java创建对象的方式

import java.io.*;
import java.lang.reflect.Constructor;
import java.util.Objects;

public class ObjectCreation {
    public static void main(String... args) throws Exception {

        // 1. Using new keyword
        Employee emp1 = new Employee();
        emp1.setName("emp1");


        // 2. Using Class class's newInstance() method
        Employee emp2 = Employee.class.newInstance();
        emp2.setName("emp2");


        // 3. Using Constructor class's newInstance() method
        Constructor<Employee> constructor = Employee.class.getConstructor();
        Employee emp3 = constructor.newInstance();
        emp3.setName("emp3");

        // 4. Using clone() method
        Employee emp4 = (Employee) emp3.clone();
        emp4.setName("emp4");


        // Serialization
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"))) {
            out.writeObject(emp4);
        }

        // 5. Using Deserialization
        Employee emp5;
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"))) {
            emp5 = (Employee) in.readObject();
            emp5.setName("emp5");
        }

        System.out.println(emp1 + ", hashcode : " + emp1.hashCode());
        System.out.println(emp2 + ", hashcode : " + emp2.hashCode());
        System.out.println(emp3 + ", hashcode : " + emp3.hashCode());
        System.out.println(emp4 + ", hashcode : " + emp4.hashCode());
        System.out.println(emp5 + ", hashcode : " + emp5.hashCode());
    }
}
class Employee implements Cloneable, Serializable {

    private static final long serialVersionUID = 1L;

    private String name;

    public Employee() { System.out.println("Employee Constructor Called..."); }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() { return Objects.hash(name); }

    @Override
    public String toString() { return String.format("Employee{name='%s'}", name); }

    @Override
    public Object clone() {
        Object obj = null;
        try {
            obj = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return obj;
    }
}

输出

Employee Constructor Called...
Employee Constructor Called...
Employee Constructor Called...
Employee{name='emp1'}, hashcode : 3117192
Employee{name='emp2'}, hashcode : 3117193
Employee{name='emp3'}, hashcode : 3117194
Employee{name='emp4'}, hashcode : 3117195
Employee{name='emp5'}, hashcode : 3117196

文章来源:5 Different Ways to Create Objects in Java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值