方法一:
首先我们建立一个Person类:
package person; import sex.Sex; public class Person{ private String name = null; private int age = 0; private Sex sex = null; private String birthday = null; public Person(){}; public Person(String name, int age, Sex sex, String birthday){ String birMatch = "\\d{4}-\\d{2}-\\d{2}"; this.name = name; this.age = age; this.sex = sex; if(birthday.matches(birMatch)){ this.birthday = birthday; } } public void setInfo(String name, int age, Sex sex, String birthday){ String birMatch = "\\d{4}-\\d{2}-\\d{2}"; this.name = name; this.age = age; this.sex = sex; if(birthday.matches(birMatch)){ this.birthday = birthday; } } public String getPerName(){ return this.name; } public int getAge(){ return this.age; } public Sex getSex(){ return this.sex; } public String getBirthday(){ return this.birthday; } public String toString(){ return this.name + ", " + this.age + " years old, " + this.sex + ", " + this.birthday; } }
再建立一个枚举Sex:
package sex; public enum Sex{ MALE, FEMALE; }
然后在Main.java中调用Person类的setInfo方法:
package main; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import person.Person; import sex.Sex; public class Main { public static void main(String[] args){ try { Class<?> cls = Class.forName("person.Person"); Object obj = cls.newInstance(); //第一个参数是被调用方法的名称,后面接着这个方法的形参类型 Method setFunc = cls.getMethod("setInfo", String.class, int.class, Sex.class, String.class); Method toStringFunc = cls.getMethod("toString"); //取得方法后即可通过invoke方法调用,传入被调用方法所在类的对象和实参,对象可以通过cls.newInstance取得 setFunc.invoke(obj, "XiaoBiqiang", 21, Sex.MALE, "1995-11-01"); System.out.println(toStringFunc.invoke(obj)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果:
XiaoBiqiang, 21 years old, MALE, 1995-11-01
方法二:
修改Person类,Sex枚举不变:
package person; import sex.Sex; public class Person{ private String name = null; private int age = 0; private Sex sex = null; private String birthday = null; public Person(){}; public Person(String name, int age, Sex sex, String birthday){ String birMatch = "\\d{4}-\\d{2}-\\d{2}"; this.name = name; this.age = age; this.sex = sex; if(birthday.matches(birMatch)){ this.birthday = birthday; } } public void setInfo(String name, int age, Sex sex, String birthday){ String birMatch = "\\d{4}-\\d{2}-\\d{2}"; this.name = name; this.age = age; this.sex = sex; if(birthday.matches(birMatch)){ this.birthday = birthday; } } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public void setSex(Sex sex){ this.sex = sex; } public void setBirthday(String birthday){ this.birthday = birthday; } public String getName(){ return this.name; } public int getAge(){ return this.age; } public Sex getSex(){ return this.sex; } public String getBirthday(){ return this.birthday; } public String toString(){ return this.name + ", " + this.age + " years old, " + this.sex + ", " + this.birthday; } }
修改Main.java:
package main; import java.lang.reflect.Method; import person.Person; import sex.Sex; public class Main { public static void main(String[] args){ try { Class<?> cls = Class.forName("person.Person"); Object obj = cls.newInstance(); setter(obj, "name", "XiaoBiqiang", String.class); setter(obj, "age", 21, int.class); setter(obj, "sex", Sex.MALE, Sex.class); setter(obj, "birthday", "1995-11-01", String.class); System.out.println((String)getter(obj, "name")); System.out.println((int)getter(obj, "age")); System.out.println((Sex)getter(obj, "sex")); System.out.println((String)getter(obj, "birthday")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void setter(Object obj, String toSet, Object value, Class<?> paraTypes){ Method target = null; try { target = obj.getClass().getMethod("set"+upperFirst(toSet), paraTypes); target.invoke(obj, value); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Object getter(Object obj, String toGet){ Method target = null; Object result = null; try { target = obj.getClass().getMethod("get"+upperFirst(toGet)); result = target.invoke(obj); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public static String upperFirst(String toUpper){ return toUpper.substring(0, 1).toUpperCase()+ toUpper.substring(1); } }
运行结果:
XiaoBiqiang 21 MALE 1995-11-01
主方法通过setter和getter方法分别设置和调用类中的属性。
建议:在类中属性要封装,通过setter和getter方法设置和获取属性