《Java基础知识》 Java创建对象的5种方法

前言

学习的过程,遇到很多情况需要通过非“new”的方式创建对象。

第一种(new)

public class Test {

    @Override
    public String toString() {
        return "===== Test =====";
    }
}
public class TestDemo {

    public static void main(String[] args) {
        Test test = new Test();   // 创建对象
        test.toString();
    }
}

第二种(class.newInstance)

public class TestDemo {

    public static void main(String[] args) {
        Test test = null;
        try {
            test = (Test) Class.forName("demo.knowledgepoints.createobject.Test").newInstance();  // 创建对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(test.toString());
    }
}

第三种(Constructor.newInstance)

public class Test {

    public Test(String str){
        System.out.println(str);
    }

    @Override
    public String toString() {
        return "===== Test =====";
    }
}
public class TestDemo {

    public static void main(String[] args) {
        try {
            Constructor<Test> constructor = Test.class.getConstructor(String.class);
            Test test = constructor.newInstance("通过构造有参构造函数创建对象");
            System.out.println(test.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果:

第四种(clone)

需要实现Cloneable接口

public class Test implements Cloneable {

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

    @Override
    public String toString() {
        return "===== Test =====";
    }
}
public class TestDemo {

    public static void main(String[] args) {
        Test test = new Test();
        try {
            Test test1 = (Test)test.clone();
            System.out.println(test1.toString());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

    }
}

运行结果:

第五种(反序列化)

public class Test implements Serializable {

    private static final long serialVersionUID = 1L;

    @Override
    public String toString() {
        return "===== Test =====";
    }
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestDemo {

    public static void main(String[] args) {
        try {
            Test test = new Test();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("demo.knowledgepoints.createobject.obj"));
            out.writeObject(test);
            out.close();

            ObjectInputStream in = new ObjectInputStream(new FileInputStream("demo.knowledgepoints.createobject.obj"));
            Test test1 = (Test) in.readObject();
            System.out.println(test1.toString());
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果:

序列化和反序列化详情参考:https://www.cnblogs.com/jssj/p/11766027.html

总结

不同的创建对象有不同的应用场景,了解这些场景对阅读源码很有帮助。

posted @ 2021-05-04 21:44  加速丨世界  阅读(363)  评论(0编辑  收藏  举报