1.使用 new 關鍵字(最常用):
objectname obj = new objectname();
2.使用反射的class類的newinstance()方法:
objectname obj = objectname.class.newinstance();
3.使用反射的constructor類的newinstance()方法:
objectname obj = objectname.class.getconstructor.newinstance();
4.使用對象克隆clone()方法:
objectname obj = obj.clone();
5.使用反序列化(objectinputstream)的readobject()方法:
objectname obj = new objectinputstream(new fileinputstream(file_name)).readobject();
代碼示例:
1.首先建立一個user類:
1 package com.example.demo.model;
2
3 import java.io.serializable;
4 import java.util.objects;
5
6 public class user implements serializable, cloneable {
7 private static final long serialversionuid = 1l;
8 private string id;
9 private string name;
10 private string phone;
11
12 public user(string id, string name, string phone) {
13 this.id = id;
14 this.name = name;
15 this.phone = phone;
16 }
17
18 public user() {
19 }
20
21 public string getid() {
22 return id;
23 }
24
25 public void setid(string id) {
26 this.id = id;
27 }
28
29 public string getname() {
30 return name;
31 }
32
33 public void setname(string name) {
34 this.name = name;
35 }
36
37 public string getphone() {
38 return phone;
39 }
40
41 public void setphone(string phone) {
42 this.phone = phone;
43 }
44
45 @override
46 public user clone() throws clonenotsupportedexception {
47 return (user) super.clone();
48 }
49
50 @override
51 public boolean equals(object o) {
52 if (this == o) return true;
53 if (o == null || getclass() != o.getclass()) return false;
54 user user = (user) o;
55 return objects.equals(id, user.id) &&
56 objects.equals(name, user.name) &&
57 objects.equals(phone, user.phone);
58 }
59
60 @override
61 public int hashcode() {
62 return objects.hash(id, name, phone);
63 }
64
65 @override
66 public string tostring() {
67 return "user{" +
68 "id='" + id + '\'' +
69 ", name='" + name + '\'' +
70 ", phone='" + phone + '\'' +
71 '}';
72 }
73 }
2.然後開始建立user對象:
1 package com.example.demo.practice;
2
3 import com.example.demo.model.user;
4
5 import java.io.fileinputstream;
6 import java.io.fileoutputstream;
7 import java.io.objectinputstream;
8 import java.io.objectoutputstream;
9
10 public class objectcreation {
11 private static final string file_name = "user.obj";
12
13 public static void main(string[] args) throws exception {
14 //方式一 使用new關鍵字
15 user user = new user("1", "張三", "135****8457");
16 system.out.println(user.tostring());
17
18 //方式二 使用class類的newinstance()方法
19 user user2 = user.class.newinstance();
20 user2.setname("李四");
21 system.out.println(user2.tostring());
22
23 //方式三 使用constructor類的newinstance()方法
24 user user3 = user.class.getconstructor().newinstance();
25 user3.setname("王五");
26 system.out.println(user3.tostring());
27
28 //方式四 使用clone()方法,前提是被克隆類必須實作cloneable接口并且重寫其clone()方法
29 user user4 = user.clone();
30 system.out.println(user4.tostring());
31 system.out.println(user == user4);
32 system.out.println(user.equals(user4));
33
34 //方式五 使用反序列化方式,調用objectinputstream對象的readobject()方法,前提是類需要實作serializable接口
35 //序列化
36 objectoutputstream oos = new objectoutputstream(new fileoutputstream(file_name));
37 oos.writeobject(user);
38 //反序列化
39 objectinputstream ois = new objectinputstream(new fileinputstream(file_name));
40 user user5 = (user) ois.readobject();
41 system.out.println(user5.tostring());
42 }
43 }
輸出結果如下:
user{id='1', name='張三', phone='135****8457'}
user{id='null', name='李四', phone='null'}
user{id='null', name='王五', phone='null'}
user{id='1', name='張三', phone='135****8457'}
false
true
user{id='1', name='張三', phone='135****8457'}
希望與廣大網友互動??
點此進行留言吧!