在反射中如何调用类中的Setter()AndGetter()方法

 

package com.alamps.classdemo;

interface China {
 public static final String NAME = "ALAMPS";
 public static final String NATIONAL = "CHINA";

 void sayChina();

 String sayHello(String name,int age);

}

public class Programmer implements China {

 private int age;
 private String name;

 /**
  * 无参
  */
 public Programmer() {
  // TODO Auto-generated constructor stub
 }

 /**
  * 有一个参数的构造
  *
  * @param name
  */
 public Programmer(String name) {
  super();
  this.name = name;
 }

 /**
  * 有二个参数的构造
  *
  * @param name
  * @param age
  */

 public Programmer(String name, int age) {
  super();
  this.name = name;
  this.age = age;
 }

 public int getAge() {
  return age;
 }

 public String getName() {
  return name;
 }

 @Override
 public void sayChina() {
  // TODO Auto-generated method stub
  System.out.println("程序员的名字是:"+NAME+",国籍是:"+NATIONAL);
 }

 @Override
 public String sayHello(String name,int age) {
  // TODO Auto-generated method stub
  return "姓名="+name+",年龄="+age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return "姓名:" + this.name + ",年龄:" + this.age;
 }
}

 

 

 

Title@Test
 // 在反射中如何调用类中的Setter()AndGetter()方法:例如:取得Programmer类中的SetterAndGetter方法
 public void InvokeSetterAndGetter() {
  Class<?> c = null;// 声明Class对象
  Object oo = null;// 声明一个对象

  try {
   c = Class.forName("com.alamps.classdemo.Programmer");// 最常用的形式
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }

  try {
   oo = c.newInstance();// 实例化操作对象
  } catch (InstantiationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  setter(oo, "name", "七仙女", String.class);
  setter(oo, "age", 18, int.class);
  System.out.print("姓名:");
  getter(oo, "name");
  System.out.print(";年龄:");
  getter(oo, "age");

 }// end of method InvokeSetterAndGetter

 // 姓名=猪八戒,年龄=8000

 public static void setter(Object oo, String att, Object value,
   Class<?> type) {// setName(String s)
  try {
   Method m = oo.getClass().getMethod("set" + initStr(att) ,type );
   try {
    m.invoke(oo, value);
   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } catch (SecurityException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public static void getter(Object oo, String att) {// getName()
  try {
   Method m = oo.getClass().getMethod("get" + initStr(att));
   try {
    System.out.print(m.invoke(oo));
   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } catch (SecurityException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public static String initStr(String old) {
  // TODO Auto-generated method stub
  String newstr = old.substring(0, 1).toUpperCase() + old.substring(1);// 将一个字符串的第一个单词大写
  return newstr;
 }v

你可能感兴趣的:(setter)