zoukankan      html  css  js  c++  java
  • java 创建对象的几种方式

    1.用new语句创建对象

    2.运用反射手段

    3.调用对象的clone()方法

    4.运用反序列化手段

    代码

            //new
            Student student = new Student();
            student.setAge("12");
            student.setClassName("三年级一班");
            System.out.println(student);
    
            //反射-Contructor
            Constructor<Student> constructor = Student.class.getConstructor();
            Student student1  = constructor.newInstance();
            System.out.println(student1);
    
            //反射-newInstance
            Student student2  = Student.class.newInstance();
            System.out.println(student2);
    
            //clone
            Student student3 = student.clone();
            System.out.println(student3);
    
            //反序列化
            FileInputStream fileInputStream = new FileInputStream("E://tmp/employee.ser");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            Student student4 = (Student) objectInputStream.readObject();
            System.out.println(student4);

  • 相关阅读:
    test
    VS dll 引用依赖
    Git配置
    编码--文字输入的前因后果
    base64相关
    异或
    UNION / UNION ALL 区别
    数据库使用规范
    chrome插件开发学习(一)
    缓存穿透 缓存雪崩 缓存并发
  • 原文地址:https://www.cnblogs.com/excellencesy/p/14382001.html
Copyright © 2011-2022 走看看