java中建立对象的五种方式

一、new函数

调用了构造函数接口

二、反射建立class

Class myclass = Class.forName("com.company.Student");
Student student = (Student)myclass.newInstance();

调用了构造函数object

三、反序列化构造函数

ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d:/yld.txt"));
Student student = (Student)objectInputStream.readObject();

须要实现Serializable接口,没有调用构造函数序列化

 

四、clone(克隆)反射

Student student = new Student("张三",23);
Student student2 = (Student)student.clone();

须要实现Cloneable接口,并重写其clone方法,没有调用构造函数方法

相关文章
相关标签/搜索