Java创建对象的5种方式

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'}

 

posted @ 2019-08-21 15:46  Eunice_Sun  阅读(476)  评论(0编辑  收藏  举报