一个demo,主要关注以下几个类和方法的使用:
Method类的:
publicMethod getMethod(String name, Class<?>... parameterTypes)
public Object invoke(Object obj, Object... args)
Class类的:
public Field getDeclaredField(String name)
Field类的:
public void set(Object obj, Object value)
1.Person3.javapackage com.yyj.Class; interface China{ // 定义China接口 public static final String NATIONAL = "China" ; // 定义全局常量 public static final String AUTHOR = "yyj" ; // 定义全局常量 public void sayChina() ; // 无参的,没有返回值的方法 public String sayHello(String name,int age) ; // 定义有两个参数的方法,并返回内容 } public class Person3 implements China{ private String name ; private int age ; public Person3(){ // 无参构造 } public Person3(String name){ this.name = name ; // 设置name属性 } public Person3(String name,int age){ this(name) ; this.age = age ; } public void sayChina(){ // 覆写方法 System.out.println("作者:" + AUTHOR + ",国籍:" + NATIONAL) ; } public String sayHello(String name,int age){ return "我是" +name + ",我今年" + age + "岁了!" ; } public void setName(String name){ this.name = name ; } public void setAge(int age){ this.age = age ; } public String getName(){ return this.name ; } public int getAge(){ return this.age ; } };
2.Invokemethods.java运行结果:package com.yyj.Class; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Invokemethods{ public static void main(String args[])throws Exception{ //实例化一个Person3类 Class<?> c=null; c=Class.forName("com.yyj.Class.Person3"); //实例化一个Person3类对象per Person3 per=null; per=(Person3)c.newInstance(); //获取Person3类的两个方法(一个无参一个有参) Method method1=c.getMethod("sayChina"); Method method2=c.getMethod("sayHello",String.class,int.class); //用per对象唤醒menthod1 method1.invoke(per); //用per对象唤醒menthod2 String rt=null; rt=(String)method2.invoke(per,"yyj",23); System.out.println(rt); //调用setter、getter方法 setter(per,"name","yd",String.class); getter(per,"name"); //实例化一个Person3类对象per2 Person3 per2=null; per2=(Person3)c.newInstance(); //获取Person3类的属性 Field namefield=null; Field agefield=null; namefield=c.getDeclaredField("name"); agefield=c.getDeclaredField("age"); //设置属性为外部可操作的 namefield.setAccessible(true); agefield.setAccessible(true); //通过field.set方法设置per2对象的属性值 namefield.set(per2,"羊羊羊"); agefield.set(per2,3); System.out.println((String)namefield.get(per2)+" "+agefield.get(per2)); } /* Object obj:要操作的对象 String att:要操作的属性 Object value:要设置的属性的值 Class<?> type要设置的属性的类型 */ //setter方法用于获取person3类的setName和setAge方法 public static void setter(Object obj,String att,Object value,Class<?> type){ try{ Method med=obj.getClass().getMethod("set"+intialStr(att),type); med.invoke(obj,value); } catch (Exception e){ e.printStackTrace(); } } //getter方法用于获取person3类的getName和getAge方法 public static void getter(Object obj,String att){ try{ Method med=obj.getClass().getMethod("get"+intialStr(att)); String rt2=(String)med.invoke(obj); System.out.println(rt2); } catch (Exception e){ e.printStackTrace(); } } //字符串大写首字母 public static String intialStr(String oldstr){ String newstr=oldstr.substring(0,1).toUpperCase()+oldstr.substring(1); return newstr; } }
我是yyj,我今年23岁了!
yd
羊羊羊 3