Java中创建对象的几种方式

java中创建对象的5种方式

作为Java攻城狮,我们每一天要创建很多对象,但是我们通常都是依赖管理系统,比如Spring去创建对象。然而久而久之我们就快忘记创建对象的方法,下面我们回顾一下:

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

1.使用new关键字

 Test test = new Test();//通过无参构造函数创建
 Test test1 = new Test(this);//this表示上下文context

    //Test对象
    public class Test{
        public Test(){

        }
        public Test(Context context){

        }
    }

2.使用Class类的newInstance方法

   //newInstance
        String className = "com.song.Test";//reference
        try {
            Test test2 = (Test)Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //或者
        try {
            Test test3 = Test.class.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

3.使用Constructor类的newInstance方法

 try {
            //拿到Constructor构造器
            Constructor<Test> constructor = Test.class.getConstructor();
            try {
                //通过构造器的newInstance返回对象
                Test Test4 = constructor.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

4.使用clone方法

需要满足两点条件
1)需要实现Cloneable接口
2)覆盖clone方法

 //CLONE
     try {
         Test test5 = (Test)test.clone();
     } catch (CloneNotSupportedException e) {
         e.printStackTrace();
     }


    //Test
    public class Test implements Cloneable{

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }

        public Test(){

        }

        public Test(Context context){

        }
    }

5.使用反序列化

调用java.io.ObjectInputStream对象的 readObject()方法返回对象
需要实现Serializable接口

 // Serialization
        String fileName = "D:/serializaton.txt";//文件的具体路径
        try {
            ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(fileName));
            o.writeObject(test);
            o.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Deserialization
        try {
            ObjectInputStream i = new ObjectInputStream(new FileInputStream(fileName));
            try {
                //通过readObject()方法返回对象
                Test test6 = (Test)i.readObject();
                i.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

参考栗子:https://blog.csdn.net/Song_74110/article/details/82745353

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值