Java 创建对象的四种方法

Java 创建对象的四种方法 @pdai

使用new 方式创建对象

 public static Worker createWorker() {  
     return new Worker();  
 }  

 public static Worker createWorker(String name, int age) {  
     return new Worker(name, age);  
 }
1
2
3
4
5
6
7

使用反射机制

/*
 * 使用反射机制,不带参数 Class 对象的 newInstance() 方法
 */  
 
    public static Worker createWorker1() {  
 
        Class clazz = null;  
        Worker worker = null;  
        try {  
            clazz = Class.forName("com.lou.creation.Worker");  
            worker = (Worker) clazz.newInstance();  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (InstantiationException e) {  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            e.printStackTrace();  
        }  
        return worker;  
    }  
 
    /*
     *  使用反射机制 , Constructor的 newInstance方法
     */  
 
    public static Worker createWorker2() {  
        Worker worker = null;  
        try {  
            Class clazz = null;  
            clazz = Class.forName("com.lou.creation.Worker");  
 
            // 获取不带参数的构造器  
            Constructor constructor = clazz.getConstructor();  
            // 使用构造器创建对象  
            worker = (Worker) constructor.newInstance();  
 
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (InstantiationException e) {  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            e.printStackTrace();  
        } catch (SecurityException e) {  
            e.printStackTrace();  
        } catch (NoSuchMethodException e) {  
            e.printStackTrace();  
        } catch (IllegalArgumentException e) {  
            e.printStackTrace();  
        } catch (InvocationTargetException e) {  
            e.printStackTrace();  
        }  
 
        return worker;  
    }  
 
    /*
     * 使用反射机制 : 带参数的构造函数创建新对象
     */  
    public static Worker createWorker3(String name, Integer age) {  
        Worker worker = null;  
        try {  
            Class clazz = null;  
            clazz = Class.forName("com.lou.creation.Worker");  
 
            // 获取不带参数的构造器  
            Constructor constructor = clazz.getConstructor(name.getClass(),  
                    age.getClass());  
            // 使用构造器创建对象  
            worker = (Worker) constructor.newInstance(name, age);  
 
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (InstantiationException e) {  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            e.printStackTrace();  
        } catch (SecurityException e) {  
            e.printStackTrace();  
        } catch (NoSuchMethodException e) {  
            e.printStackTrace();  
        } catch (IllegalArgumentException e) {  
            e.printStackTrace();  
        } catch (InvocationTargetException e) {  
            e.printStackTrace();  
        }  
        return worker;  
    }  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

序列化和反序列化创建对象

    /*
     * 使用序列化和反序列化创建对象,这种方式其实是根据既有的对象进行复制,这个需要事先将可序列化的对象线存到文件里
     */  
    @SuppressWarnings("resource")  
    public static Worker createWorker4(String objectPath) {  
        ObjectInput input = null;  
        Worker worker = null;  
        try {  
            input = new ObjectInputStream(new FileInputStream(objectPath));  
            worker = (Worker) input.readObject();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        }  
        return worker;  
    }  
 
    /*
     * 将创建的对象存入到文件内
     */  
    public static void storeObject2File(String objectPath) {  
        Worker worker = new Worker();  
        ObjectOutputStream objectOutputStream;  
        try {  
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(  
                    objectPath));  
            objectOutputStream.writeObject(worker);  
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

深拷贝

/*
     * 使用对象的 深拷贝进行复制,创建对象
     */  
    public static Worker createWorker5(Worker worker) {  
        return (Worker) worker.clone();  
    }
1
2
3
4
5
6
  • worker
import java.io.Serializable;  
 
public class Worker implements Cloneable,Serializable {  
 
    private static final long serialVersionUID = 1L;  
    private String name;  
    private int age;  
      
    public Worker()  
    {  
        this.name = "";  
        this.age = 0;  
    }  
      
    public Worker(String name,int age)  
    {  
        this.name = name;  
        this.age = age;  
    }  
      
    public void work()  
    {  
        System.out.println(name +"is working");  
    }  
      
    public Worker clone()  
    {  
        Worker worker = null;  
        try {  
            return (Worker) super.clone();  
        } catch (CloneNotSupportedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return worker;  
    }  
}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

参考

http://blog.csdn.net/luanlouis/article/details/18228199

联系我

添加@pdai微信

PS:添加时请备注Java全栈,谢谢!