测试类:
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();
}
}
原文:https://www.cnblogs.com/m987/p/14730331.html