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

时间:2021-10-22 07:58:14

定义Student类

 package org;

 public class Student {
private String _name = null;
private int _age = -;
private int _score = -; public Student()
{ } public Student(String name, int age, int score)
{
_name = name;
_age = age;
_score = score;
} public void setName(String name)
{
_name = name;
} public String getName()
{
return _name;
} public void setAge(int age)
{
_age = age;
} public int getAge()
{
return _age;
} public void setScore(int score)
{
_score = score;
} public int getScore()
{
return _score;
} public void set(Student stu)
{
setName(stu._name);
setAge(stu._age);
setScore(stu._score);
} public String toString()
{
return "Name: " + _name + ", age: " + _age + ", score: " + _score;
}
}

保存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

 package org;
import java.lang.reflect.Method; public class InvokeProxy {
public static String init_metName(String met_name)
{
return met_name.substring(, ).toUpperCase() + met_name.substring();
} public static void setter(Object obj, String met_name, Object value, Class<?> type)
{
try {
Method met = obj.getClass().getMethod("set" + init_metName(met_name), type);
met.invoke(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
} /*
public static Object getter(Object obj, String met_name)
{
Object result = null; try {
Method met = obj.getClass().getMethod("get" + init_metName(met_name));
result = met.invoke(obj);
} catch (Exception e) {
e.printStackTrace();
} return result;
}
*/
}
 package org;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method; public class tmp {
public static void main(String[] args)
{
Class<?> c0 = null;
Class<?> c1 = null;
String obj_name = "org.Student";
String save_name = "org.SaveStudents";
String file_name = "F:\\Code\\java\\text\\test15.txt"; try {
c0 = Class.forName(obj_name);
c1 = Class.forName(save_name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} String name = null;
int age = -1;
int score = -1; // Inputs
{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
OutputStream os = System.out; try {
os.write("Input name: ".getBytes());
name = buf.readLine(); os.write("Input age: ".getBytes());
age = Integer.parseInt(buf.readLine()); os.write("Input score: ".getBytes());
score = Integer.parseInt(buf.readLine());
} catch (Exception e) {
e.printStackTrace();
}
} // instance Student
Student stu = null;
try {
stu = (Student)c0.newInstance();
} catch (Exception e) {
e.printStackTrace();
} // set
{
InvokeProxy.setter(stu, "name", name, String.class);
InvokeProxy.setter(stu, "age", age, int.class);
InvokeProxy.setter(stu, "score", score, int.class);
} // instance SaveStudents and invoke save() method
SaveStudents ss = null;
Constructor<?> con1 = null;
try {
con1 = c1.getConstructor(String.class);
ss = (SaveStudents)con1.newInstance(file_name);
Method met = ss.getClass().getMethod("save", Student.class);
met.invoke(ss, stu); } catch (Exception e) {
e.printStackTrace();
}
}
}