• Java 创建对象的几种方式


    测试类:

    package com.baidou.demo01;
    
    import org.junit.Test;
    
    import java.io.*;
    import java.lang.reflect.*;
    
    /**
     * @ClassName: NewObjectTest
     * @Description: 创建对象
     * @Author: lenovo
     * @Date: 2021/5/4 19:34
     * Version: 1.0
     */
    public class NewObjectTest {
    
    
        // 1、使用 new关键字创建对象
        @Test
        public void test1() {
            Student zhangsan = new Student("张三", 18);
            System.out.println(zhangsan); // Student{name='张三', age=18}
        }
    
        // 2、反射: 使用Class类的newInstance方法
        @Test
        public void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
            Class<?> aClass = Class.forName("com.baidou.demo01.Student");
            Student student = (Student) aClass.newInstance(); // 通过这个newInstance方法调用无参构造器创建对象
            student.setAge(18);
            student.setName("小明");
            System.out.println(student); // Student{name='小明', age=18}
        }
    
        // 反射
        @Test
        public void test3() throws IllegalAccessException, InstantiationException {
            Class<Student> clazz = Student.class;
            Student student = clazz.newInstance();
            student.setAge(18);
            student.setName("花无缺");
            System.out.println(student); // Student{name='花无缺', age=18}
        }
    
        // 反射: 使用Constructor类的newInstance方法
        @Test
        public void test4() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
            Constructor<Student> constructor = Student.class.getConstructor();  // API位于: java.lang.reflect.Constructor;
            Student student = constructor.newInstance(); // 通过这个newInstance方法调用有参数的和私有的构造函数。
            student.setAge(18);
            student.setName("小花");
            System.out.println(student); // Student{name='小花', age=18}
    
        }
    
    
        // 3、使用clone方法,我们首先需要将实体类实现Cloneable接口,然后重写clone方法
        @Test
        public void test5() throws CloneNotSupportedException {
            // 无论何时我们调用一个对象的clone方法,jvm就会创建一个新的对象,将前面对象的内容全部拷贝进去。用clone方法创建对象并不会调用任何构造函数。
            Student zhangsan = new Student("张三", 18);
            Student lisi = (Student) zhangsan.clone();
            lisi.setAge(18);
            lisi.setName("李四");
            System.out.println(lisi); // Student{name='李四', age=18}
        }
    
        // 4、使用反序列化,需要将实体类实现Serializable接口。
        // 序列化是指将java对象转化成二进制形式输出,反序列化顾名思义就是将二进制流再转为对象的过程。
        @Test
        public void test6() throws IOException, ClassNotFoundException {
            // 1、序列化
            // 常用的序列化工具: JDK序列化 protobuf hession xstream JSON
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("a.txt"));
            objectOutputStream.writeObject(new Student("赵六", 18));
            objectOutputStream.close();
            // �� sr com.baidou.demo01.StudentT�R]by$� I ageL namet Ljava/lang/String;xp   t 赵六
    
            // 2、反序列化
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("a.txt"));
            Student zhaoliu = (Student) objectInputStream.readObject();
            System.out.println(zhaoliu); // Student{name='赵六', age=18}
            objectInputStream.close();
        }
    
    
        // 5、使用动态代理
        @Test
        public void test7() {
    
        }
    }
    

    实体类:

    package com.baidou.demo01;
    
    
    import java.io.Serializable;
    
    /**
     * @ClassName: Student
     * @Description: 学生实体类
     * @Author: lenovo
     * @Date: 2021/5/4 19:31
     * Version: 1.0
     */
    public class Student implements Cloneable, Serializable {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Student() {
            System.out.println("Student()");
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    

    image

  • 相关阅读:
    IIS6为应用程序池**提供服务的进程与 World Wide Web Publishing 服务通信时遇到致命错误
    C# WINFORM 强制让窗体获得焦点
    数据库性能优化详解
    sqlserver 2008 修改表结构不能保存
    SQL性能优化
    aspnet打印 设置(页脚 一些数据显示在打印页的最下面)
    图片转成byte数组
    在配置文件中定义初始值然后读取
    DOM编程
    html动态显示时间
  • 原文地址:https://www.cnblogs.com/m987/p/14730331.html
Copyright © 2020-2023  润新知