在本篇文章中,将介绍5种创建Java对象的方式。
1. 使用new关键字
package com.idea.test;
public class Student {
private String userName;
private String nickName;
public Student(String userName, String nickName) {
this.userName = userName;
this.nickName = nickName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "Student{" +
"userName='" + userName + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
public class TestNewStudent{
public static void main(String[] args){
Student zhangsan = new Student("Zhangsan","张三");
Student lisi = new Student();
lisi.setName("Lisi");
lisi.setNickname("李四");
}}
2. 使用Class类的newInstance()方法
首先,我们可以通过Class.forName()方法动态加载目标类,然后再调用newInstance()方法,动态实例化对象。
package com.idea.test;
public class Student {
private String userName;
private String nickName;
public Student(String userName, String nickName) {
this.userName = userName;
this.nickName = nickName;
}
public Student() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "Student{" +
"userName='" + userName + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
try {
Class clz=Class.forName("com.idea.test.Student");
Student student1=(Student)clz.newInstance();
student1.setUserName("庞翔");
student1.setNickName("大个");
System.out.println("Class 的newInstance方法创建对象"+student1);
} catch (Exception e) {
e.printStackTrace();
}
3. 使用Constructor.newInstance()方法
与Class类的newInstance()方法相似,我们还可以使用java.lang.reflect.Constructor类的newInstance()方法来创建新的对象。
package com.idea.test;
public class Student {
private String userName;
private String nickName;
public Student(String userName, String nickName) {
this.userName = userName;
this.nickName = nickName;
}
public Student() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "Student{" +
"userName='" + userName + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
Constructor<Student> constructorStudent;
try{
constructorStudent=Student.class.getConstructor();
Student student2=constructorStudent.newInstance();
student2.setUserName("傻逼");
System.out.println("Contructor newInstance()创建对象"+student2);
}catch (Exception e){
e.printStackTrace();
}
4. 使用反序列化
如果要使用反序列化创建对象,则目标类首先要实现Serializable接口。在本次示例中,我们先创建一个Student对象,并将其保存到data.txt文件中,然后在通过反序列化操作,读取data.txt中的数据,并创建新的对象。
package com.idea.test;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 7497761277861642186L;
private String userName;
private String nickName;
public Student(String userName, String nickName) {
this.userName = userName;
this.nickName = nickName;
}
public Student() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "Student{" +
"userName='" + userName + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
String filePath="D:\\data.txt";
Student stu=new Student("序列化","反序列化");
FileOutputStream fileOutputStream=null;
try{
fileOutputStream=new FileOutputStream(filePath);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(stu);
objectOutputStream.flush();
objectOutputStream.close();
FileInputStream fileInputStream=new FileInputStream(filePath);
ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
Student stu1=(Student) objectInputStream.readObject();
System.out.println("通过反序列化创建对象"+stu1);
}catch(Exception e){
}
5. 使用对象克隆创建新的对象
clone()方法可以创建现有对象的副本,但在使用clone()方法前,需保证模板类实现了Cloneable接口,Cloneable接口也是一个标记类接口。