方法一:new關鍵字
定義 Person 類如下:
package com.ph.object;
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public void selfIntroduction() {
System.out.println("我是" + name + ",今年" + age + "歲。");
}
}
Test 類如下:
package com.ph.object;
public class Test {
public static void main(String[] args) {
// new關鍵字創建
Person liuBei = new Person("劉備", 50);
System.out.println(liuBei);
liuBei.selfIntroduction();
}
}
輸出:
com.ph.object.Person@4554617c
我是劉備,今年50歲。
方法二:反射
Person 類保持不變, Test 改爲:
package com.ph.object;
import java.lang.reflect.Constructor;
public class Test {
public static void main(String[] args) {
String className = "com.ph.object.Person";
Person liBai = null;
try {
// 反射創建
Class<?> personClass = Class.forName(className);
Constructor<?> personConstructor = personClass.getConstructor(String.class, Integer.class);
liBai = (Person) personConstructor.newInstance("李白", 60);
} catch (ReflectiveOperationException e) {
System.out.println("反射創建對象失敗");
}
System.out.println(liBai);
if (liBai != null) {
liBai.selfIntroduction();
}
}
}
輸出:
com.ph.object.Person@4554617c
我是李白,今年60歲。
方法三:clone方法
使用 clone 方法,需要被克隆的類實現 Cloneable 接口,並重寫 Object 類的 clone 方法。
package com.ph.object;
public class Person implements Cloneable {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public void selfIntroduction() {
System.out.println("我是" + name + ",今年" + age + "歲。");
}
@Override
public Person clone() {
Person clone = null;
try {
clone = (Person) super.clone();
} catch (CloneNotSupportedException e) {
System.err.println("克隆創建對象失敗");
}
return clone;
}
}
Test 類改爲:
package com.ph.object;
public class Test {
public static void main(String[] args) {
Person zhuGe = new Person("諸葛亮", 70);
System.out.println(zhuGe);
zhuGe.selfIntroduction();
// 以上部分是初始條件
// clone創建
Person cloneZhuGe = zhuGe.clone();
System.out.println(cloneZhuGe);
cloneZhuGe.selfIntroduction();
}
}
輸出:
com.ph.object.Person@4554617c
我是諸葛亮,今年70歲。
com.ph.object.Person@74a14482
我是諸葛亮,今年70歲。
方法四:反序列化
使用序列化,需要被序列化的類實現 Serializable 接口,並添加 serialVersionUID 字段。
package com.ph.object;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public void selfIntroduction() {
System.out.println("我是" + name + ",今年" + age + "歲。");
}
}
Test 改爲:
package com.ph.object;
import java.io.*;
public class Test {
public static void main(String[] args) {
String pathname = "src/com/ph/object/person.txt";
File file = new File(pathname);
// 序列化,將對象寫入文件
try {
OutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(new Person("典韋", 80));
os.close();
oos.close();
} catch (IOException e) {
System.err.println("序列化失敗");
}
// 以上是初始條件
Person dianWei = null;
try {
InputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(is);
dianWei = (Person) ois.readObject();
is.close();
ois.close();
} catch (Exception e) {
System.err.println("反序列化創建對象失敗");
}
System.out.println(dianWei);
if (dianWei != null) {
dianWei.selfIntroduction();
}
}
}
輸出:
com.ph.object.Person@15aeb7ab
我是典韋,今年80歲。
參考資料
1、java創建對象的四種方式 - 雲–澈
2、一篇文章看懂java反射機制(反射實例化對象-反射獲得構造方法,獲得普通方法,獲得字段屬性) - 多情劍客無情劍;