在本篇文章中,將介紹5種創建JAVA對象的方式。類是創建對象的基本模板,接下來將介紹5種不同的方式,利用Java類來實例化Java對象。
1. 使用new關鍵字
? 采用new關鍵字實例化對象是Java中最為常見的方法,下面是采用new關鍵字實例化對象的示例。在開始之前,我們先準備好一個Java類:Student.java。
package com.ramostear.oops;
public class Student{
private String name;
private String nickname;
public Student(String name,String nickname){
this.name = name;
this.nickname = nickname;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getNickname(){
return nickname;
}
public void setNickname(String nickname){
this.nickname = nickname;
}
}
接下來,我們通過new關鍵字來創建幾個學生:
package com.ramostear.oops;
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()方法,動態實例化對象。同理,在開始之前,我們需要準備一個目標類:Student.java。
package com.ramostear.oops;
public class Student{
private String name = "Zhangsan";
private String nickname = "張三";
public Student(){
super();
}
public Student(String name,String nickname){
super();
this.name = name;
this.nickname = nickname;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getNickname(){
return nickname;
}
public void setNickname(String nickname){
this.nickname = nickname;
}
}
接下來,將演示如何通過newInstance()方法來創建新的對象:
package com.ramostear.oops;
public class TestUseNewInstanceMethod{
public static void main(String[] args){
try{
String className = "com.ramostear.oops.Student";
Class clz = Class.forName(className);
Student student = (Student) clz.newInstance();
System.out.println("student name :"+ student.getName()+", nickname: "+student.getNickname());
}catch(InstantiationException | IllegalAccessException | ClassNotFoundException e){
e.printStackTrace();
}
}
}
運行main()方法,將輸出如下信息:
student name :Zhangsan, nickname:張三
3. 使用Constructor.newInstance()方法
? 與Class類的newInstance()方法相似,我們還可以使用java.lang.reflect.Constructor類的newInstance()方法來創建新的對象。同樣,我們先準備一個目標類:
package com.ramostear.oops;
public class Student{
private String name = "Constructor";
private String nickname = "構造器";
public Student(){
super();
}
public Student(String name,String nickname){
super();
this.name = name;
this.nickname = nickname;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getNickname(){
return nickname;
}
public void setNickname(String nickname){
this.nickname = nickname;
}
}
接下來,我們看看如何使用Constructor的newInstance()方法創建新的對象:
package com.ramostear.oops;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class UseConstructorNewInstanceMethod{
public static void main(String[] args){
Constructor<Student> studentConstructor;
try{
studentConstructor = Student.class.getConstructor();
Student student = studentConstructor.newInstance();
System.out.println("student name :"+ student.getName()+", nickname: "+student.getNickname());
}catch(InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException |
NoSuchMethodException | SecurityException e){
e.printStackTrace();
}
}
}
執行main()方法,將在控制臺輸出如下信息:
student name:Constructor, nickname:構造器
4. 使用反序列化
? 如果要使用反序列化創建對象,則目標類首先要實現Serializable接口。Serializable是一個標記接口。在本次示例中,我們先創建一個Student對象,并將其保存到data.txt文件中,然后在通過反序列化操作,讀取data.txt中的數據,并創建新的對象。
package com.ramostear.oops;
import java.io.Serializable;
public class Student implements Serializable{
private String name;
private String nickname;
public Student() {
super();
}
public Student(String name, String nickname) {
super();
this.name = name;
this.nickname = nickname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
接下來,將通過一個簡單的示例,演示如何使用反序列化的方式創建新的對象:
package com.ramostear.oops;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class StudentDemo {
public static void main(String[] args) {
String filePath = "data.txt";
Student student1 = new Student("Deserialization", "反序列化");
try {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
outputStream.writeObject(student1);
outputStream.flush();
outputStream.close();
FileInputStream fileInputStream = new FileInputStream(filePath);
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
Student student2 = (Student) inputStream.readObject();
inputStream.close();
System.out.println("Student name:"+student2.getName()+", nickname:"+student2.getNickname());
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
執行上述代碼,將在控制臺輸出如下信息:
Student name:Deserialization,nickname:反序列化
5. 使用對象克隆創建新的對象
? clone()方法可以創建現有對象的副本,但在使用clone()方法前,需保證模板類實現了Cloneable接口,Cloneable接口也是一個標記類接口。下面是使用克隆方式創建新對象的完整示例:
package net.javaguides.corejava.oops;
import java.io.Serializable;
public class Student implements Cloneable {
private String name;
private String nickname;
public Student() {
super();
}
public Student(String name, String nickname) {
super();
this.name = name;
this.nickname = nickname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public static void main(String args[]) {
Student stu1 = new Student("Clone", "克隆");
try {
Student stu2 = (Student) stu1.clone();
System.out.println("Student name :"+stu2.getName()+",nickname:"+stu2.getNickanme());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
運行上述代碼,控制臺將輸出如下信息:
Student name: Clone,nickname:克隆