• 《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

    总结

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

    This moment will nap, you will have a dream; But this moment study,you will interpret a dream.
  • 相关阅读:
    关于Linux_CentOS8.0安装软件时遇到的坑
    Docker基本命令
    mobilenetV2 +ssd相关文章梳理汇总
    FCN.py语义分割脚本代码小白级注释
    剑指 Offer 09. 用两个栈实现队列
    剑指 Offer 05. 替换空格
    剑指 Offer 04. 二维数组中的查找
    剑指 Offer 10 I. 斐波那契数列
    剑指 Offer 30. 包含min函数的栈
    剑指 Offer 06. 从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/jssj/p/14730361.html
Copyright © 2020-2023  润新知