本帖最后由 zl78365336 于 2013-12-8 17:16 编辑
使用反射方式调用setName方法对名称进行设置
其中p2这段有什么问题,求指点!
- import java.io.Serializable;
- import java.lang.reflect.*;
- public class Test2 {
- public static void main(String[] args) throws Exception {
- Class clazz = Person.class;
- Constructor con = clazz.getConstructor(String.class,int.class);
- Person p1 = (Person)con.newInstance("zz",25);
- System.out.println(p1);
-
- Person p2 = (Person)clazz.getConstructor().newInstance(null);
- clazz.getMethod("setName", String.class).invoke(p2, "zl");
- System.out.println(p2);
-
- Field field = clazz.getDeclaredField("age");
- field.setAccessible(true);
- if(field.getType()==int.class)
- field.set(p2,20);
- System.out.println(field.get(p2));
-
- }
- }
- @SuppressWarnings("serial")
- //实现标记接口
- class Person implements Serializable {
- private String name;
- private int age;
-
- public void getName(String name){
- this.name = name;
- }
- public String setName(){
- return name;
- }
-
- public void getAge(int age){
- this.age = age;
- }
- public int setAge(){
- return age;
- }
- public Person(){}
- public Person(String name,int age){
- super();
- this.name = name;
- this.age = age;
- }
- //覆盖toString()方法
- @Override
- public String toString(){
- return "name: "+this.name+"; age: "+this.age;
- }
- }
复制代码
|