java创建对象的5种方式
2017-04-26 19:04
525 查看
1.new
2.使用反射Class.forName(“com.cn.mybatisDemo.util.Student”).newInstance()或Student.class.newInstance();
3.使用构造器
Constructor constructor=Student.class.getConstructor();
Student newInstance = constructor.newInstance();
或Student student4 = Student.class.getConstructor().newInstance();
4.clone()
必须在实体类中实现Cloneable接口,重写clone()方法,并将其改为public修饰,Cloneable接口和我们在编写IO程序的时候序列化接口一样,只是一个标志,这个接口是不包含任何方法的,这个标志主要是为了检测Object类中的clone方法,若我们定义的类想要实现拷贝功能,但是没有实现该接口而调用Object的clone方法,那么就会出现语句中catch块里面的异常错误,抛出CloneNotSupportedException。
5.使用反序列化的方式,切记实体需要实现Serializable接口
import java.io.Serializable;
public class Student implements Cloneable,Serializable{
private int id;
private String name;
private int age;
private String sex;
private String content;
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class TestObjectCreate {
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
ClassNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, CloneNotSupportedException, FileNotFoundException, IOException {
}
2.使用反射Class.forName(“com.cn.mybatisDemo.util.Student”).newInstance()或Student.class.newInstance();
3.使用构造器
Constructor constructor=Student.class.getConstructor();
Student newInstance = constructor.newInstance();
或Student student4 = Student.class.getConstructor().newInstance();
4.clone()
必须在实体类中实现Cloneable接口,重写clone()方法,并将其改为public修饰,Cloneable接口和我们在编写IO程序的时候序列化接口一样,只是一个标志,这个接口是不包含任何方法的,这个标志主要是为了检测Object类中的clone方法,若我们定义的类想要实现拷贝功能,但是没有实现该接口而调用Object的clone方法,那么就会出现语句中catch块里面的异常错误,抛出CloneNotSupportedException。
5.使用反序列化的方式,切记实体需要实现Serializable接口
import java.io.Serializable;
public class Student implements Cloneable,Serializable{
private int id;
private String name;
private int age;
private String sex;
private String content;
public Student() { } public Student(int id, String name, int age, String sex, String content) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; this.content = content; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public Student clone() throws CloneNotSupportedException { return (Student)super.clone(); }
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class TestObjectCreate {
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
ClassNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, CloneNotSupportedException, FileNotFoundException, IOException {
Student student1 = new Student(); System.out.println(student1); Student student2 = (Student) Class.forName("com.cn.mybatisDemo.util.Student").newInstance(); System.out.println(student2); Student student3 = Student.class.newInstance(); System.out.println(student3); Constructor<Student> constructor=Student.class.getConstructor(); Student newInstance = constructor.newInstance(); System.out.println(newInstance); Student student4 = Student.class.getConstructor().newInstance(); System.out.println(student4); Student student5 = student4.clone(); System.out.println(student5); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("data")); objectOutputStream.writeObject(student5); ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("data")); Student student6 = (Student) inputStream.readObject(); System.out.println(student6); }
}
相关文章推荐
- Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java中有5种创建对象的方式
- Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java技术——Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java 中创建对象的5种方式
- Java中创建对象的5种方式
- 【java类与对象】Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java中创建对象的5种方式
- Java技术——Java中创建对象的5种方式
- java创建对象的5种方式
- Java中创建对象的5种方式
- Java中创建对象的5种方式总结