使用java反射机制编写Student类并保存

定义Student类

 1 package org;
 2 
 3 public class Student {
 4     private String _name = null;
 5     private int _age = -1;
 6     private int _score = -1;
 7     
 8     public Student()
 9     {
10         
11     }
12     
13     public Student(String name, int age, int score)
14     {
15         _name = name;
16         _age = age;
17         _score = score;
18     }
19     
20     public void setName(String name)
21     {
22         _name = name;
23     }
24     
25     public String getName()
26     {
27         return _name;
28     }
29     
30     public void setAge(int age)
31     {
32         _age = age;    
33     }
34     
35     public int getAge()
36     {
37         return _age;
38     }
39     
40     public void setScore(int score)
41     {
42         _score = score;
43     }
44     
45     public int getScore()
46     {
47         return _score;
48     }
49     
50     public void set(Student stu)
51     {
52         setName(stu._name);
53         setAge(stu._age);
54         setScore(stu._score);
55     }
56     
57     public String toString()
58     {
59         return "Name: " + _name + ", age: " + _age + ", score: " + _score; 
60     }
61 }

 

保存Student类的信息  SaveStudents类

package org;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class SaveStudents {
    private String _file_name = null;
    
    public SaveStudents(String file_name)
    {
        _file_name = file_name;
    }
    
    public boolean save(Student stu) throws Exception
    {
        OutputStream os = new FileOutputStream(_file_name, true);
        boolean flag = false;        //    是否保存成功的标志
        
        if (null != stu.getName()) {
            os.write(stu.toString().getBytes());
            os.close();
            flag = true;
        }
        
        return flag;
    }
}

 

代理调用类InvokeProxy

 1 package org;
 2 import java.lang.reflect.Method;
 3 
 4 public class InvokeProxy {
 5     public static String init_metName(String met_name)
 6     {
 7         return met_name.substring(0, 1).toUpperCase() + met_name.substring(1);
 8     }
 9     
10     public static void setter(Object obj, String met_name, Object value, Class<?> type)
11     {
12         try {
13             Method met = obj.getClass().getMethod("set" + init_metName(met_name), type);
14             met.invoke(obj, value);
15         } catch (Exception e) {
16             e.printStackTrace();
17         }
18     }
19     
20     /*
21     public static Object getter(Object obj, String met_name)
22     {
23         Object result = null;
24         
25         try {
26             Method met = obj.getClass().getMethod("get" + init_metName(met_name));
27             result = met.invoke(obj);
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31         
32         return result;
33     }
34     */
35 }

 

 1 package org;
 2 import java.io.BufferedReader;
 3 import java.io.InputStreamReader;
 4 import java.io.OutputStream;
 5 import java.lang.reflect.Constructor;
 6 import java.lang.reflect.Method;
 7 
 8 public class tmp {
 9     public static void main(String[] args)
10     {
11         Class<?> c0 = null;
12         Class<?> c1 = null;
13         String obj_name = "org.Student";
14         String save_name = "org.SaveStudents";
15         String file_name = "F:\\Code\\java\\text\\test15.txt";
16         
17         try {
18             c0 = Class.forName(obj_name);
19             c1 = Class.forName(save_name);
20         } catch (ClassNotFoundException e) {
21             e.printStackTrace();
22         }
23         
24         String name = null;
25         int age = -1;
26         int score = -1;
27         
28         //    Inputs
29         {
30             BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
31             OutputStream os = System.out;
32             
33             try {
34                 os.write("Input name: ".getBytes());
35                 name = buf.readLine();
36                 
37                 os.write("Input age: ".getBytes());
38                 age = Integer.parseInt(buf.readLine());
39                 
40                 os.write("Input score: ".getBytes());
41                 score = Integer.parseInt(buf.readLine());
42             } catch (Exception e) {
43                 e.printStackTrace();
44             }
45         }
46         
47         //    instance Student
48         Student stu = null;
49         try {
50             stu = (Student)c0.newInstance();
51         } catch (Exception e) {
52             e.printStackTrace();
53         }
54         
55         //    set
56         {
57             InvokeProxy.setter(stu, "name", name, String.class);
58             InvokeProxy.setter(stu, "age", age, int.class);
59             InvokeProxy.setter(stu, "score", score, int.class);
60         }
61         
62         //    instance SaveStudents and invoke save() method
63         SaveStudents ss = null;
64         Constructor<?> con1 = null;
65         try {
66             con1 = c1.getConstructor(String.class);
67             ss = (SaveStudents)con1.newInstance(file_name);
68             Method met = ss.getClass().getMethod("save", Student.class);
69             met.invoke(ss, stu);
70             
71         } catch (Exception e) {
72             e.printStackTrace();
73         }
74     }
75 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。