java创建对象的5种方式
来源:互联网 发布:网络布线 报价 编辑:程序博客网 时间:2024/06/06 06:45
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;
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;}@Overridepublic 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创建对象的四种方式
- Java创建对象的几种方式
- java创建对象的几种方式
- Java创建对象的几种方式
- java创建对象的几种方式
- Java 创建对象的四种方式
- java创建对象的几种方式
- JAVA创建对象的4种方式
- java对象创建的几种方式
- Java创建对象的四种方式
- java 创建对象的几种方式
- Java创建对象的四种方式
- 第四单元
- 带有注解的servlet的生命周期.
- css绝对定位
- 2017.4.26 组合数问题 思考记录
- python3 分支界限法解决作业分配问题
- java创建对象的5种方式
- ZOJ 3959 Problem Preparation 【】
- 时钟
- 移动端UI自动化Appium测试——DesiredCapabilities参数配置及含义
- OCLint 安装与使用
- Linux学习笔记之浅谈init命令
- Python基础数据结构之dict
- fusionChart 中 总是遇到 loading chart. please wait.
- 大白 计算几何专题 凸包、半平面交、平面区域 部分例题练习题总结