import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.Constructor;
public class Student {
public void method(){
System.out.println("Hello");
}
public static void main(String[] args) throws Exception {
//通过new调用了无参构造创建对象
Student student1 = new Student();
student1.method();
//通过Class类中的newInstance创建对象
Student student2 = Student.class.newInstance();
student2.method();
//通过Constructor中的newInstance创建对象
Constructor<Student> constructor = Student.class.getConstructor();
Student student3 = constructor.newInstance();
student3.method();
//通过clone创建对象
Student student4 =(Student) student3.clone();
student4.method();
//通过序列化和反序列化创建对象
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("文件名"));
Student student5 = (Student)objectInputStream.readObject();
student5.method();
}
}