java 创建对象的5种方法

java 创建对象的5种方法

参考文章:https://www.cnblogs.com/wxd0108/p/5685817.html

其中调用构造函数方法有三种:

(1)使用new关键字 

(2)使用Class类的newInstance方法 

(3)使用Constructor类的newInstance方法 

没有调用构造函数两种:

(4)使用clone方法

(5)使用反序列化

public static void main(String... args) throws Exception {
        // By using new keyword
        Employee emp1 = new Employee();
        emp1.setName("Naresh");
        System.out.println(emp1 + ", hashcode : " + emp1.hashCode());

        System.out.println("==========================================================================================");

        // By using Class class's newInstance() method
        Employee emp2 = (Employee) Class.forName("TestDemo.Employee")
                .newInstance();
        // Or we can simply do this
         Employee emp22 = Employee.class.newInstance();
        emp22.setName("hello Spark !");
        emp2.setName("Rishi");
        System.out.println(emp22 +", hashcode : " + emp22.hashCode());
        System.out.println(emp2 + ", hashcode : " + emp2.hashCode());
        System.out.println(emp1 + ", hashcode : " + emp1.hashCode());
        System.out.println("==========================================================================================");
        // By using Constructor class's newInstance() method
        Constructor<Employee> constructor = Employee.class.getConstructor();
        Employee emp3 = constructor.newInstance();
        emp3.setName("Yogesh");
        System.out.println(emp3 + ", hashcode : " + emp3.hashCode());
        System.out.println(emp1 + ", hashcode : " + emp1.hashCode());
        System.out.println("==========================================================================================");
        // By using clone() method
        Employee emp4 = (Employee) emp3.clone();
        emp4.setName("Atul");
        System.out.println(emp4 + ", hashcode : " + emp4.hashCode());
        System.out.println(emp3 + ", hashcode : " + emp3.hashCode());
        System.out.println("==========================================================================================");
        // By using Deserialization
        // Serialization
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"));
        out.writeObject(emp4);
        out.close();
        //Deserialization
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
        Employee emp5 = (Employee) in.readObject();
        in.close();
        emp5.setName("Akash");
        System.out.println(emp5 + ", hashcode : " + emp5.hashCode());
        System.out.println(emp4 + ", hashcode : " + emp4.hashCode());
        System.out.println("==========================================================================================");
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值