对于java反射机制的描述可参考其他网站的解释,这里主要是对其中的对内部函数、私有变量和构造函数的用法举例
package main;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by bigzhou on 2016/10/23 0023.
*/
public class Person {
public static void main(String[]arg) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, ClassNotFoundException {
System.out.println("\n***************练习内部函数调用*****************");
System.out.println("有一个参数的:");
Class class11 = Person.class;
Method method11 = class11.getDeclaredMethod("showOutName", String.class);
method11.invoke(class11.newInstance(),"chenzhen");
System.out.println("有多个参数的:");
Class class12 = Person.class;
Method method12 = class12.getDeclaredMethod("showOut",new Class[]{String.class,int.class});
method12.invoke(class12.newInstance(),"chenzhen",23);
System.out.println("静态内部函数:");
Class class13 = Person.class;
Method method13 = class13.getDeclaredMethod("staticFunc",String.class);
method13.invoke(class13,"statictest"); //这里注意下调用的不是类实例而是类对象
System.out.println("**************练习私有变量调用*****************");
Class class21 = Person.class;
Person person21 = (Person) class21.newInstance();
Field field21 = class21.getDeclaredField("name");
field21.setAccessible(true); //这个是专门处理私有类型的,需要设置为true
field21.set(person21,"huoyuanjia");
person21.showName();
System.out.println("**************练习私有构造函数***************");
System.out.println("无参数的:");
Constructor con31 = Person.class.getDeclaredConstructor();
con31.setAccessible(true);
Person f31 = (Person)con31.newInstance();
f31.showAll();
System.out.println("有多个参数的:");
Constructor con32 = Person.class.getDeclaredConstructor(new Class[]{int.class,String.class});
con32.setAccessible(true);
Person f2 = (Person) con32.newInstance(new Object[]{23,"zhou"});
f2.showAll();
System.out.println("有一个参数的:");
Constructor con3 = Person.class.getDeclaredConstructor(int.class);
con3.setAccessible(true);
Person f3 = (Person) con3.newInstance(23);
f3.showAll();
}
private int age = 0;
private String name = "asdf";
Person(){}
Person(int age, String name){
this.age = age;
this.name = name;
}
Person(int age){
this.age = age;
}
void showAll(){
System.out.println("name :"+name+"\naget :"+age+"\n");
}
public void showAge(){
System.out.println("age :"+age);
}
public void showName(){
System.out.println("name :"+name+"\n");
}
public void showOutName(String outname){
System.out.println("name :"+outname+"\n");
}
public void showOut(String outname, int outage){
System.out.println("name :"+outname+"\nage :"+outage+"\n");
}
public static void staticFunc(String str){
System.out.println("static function: "+str+"\n");
}
}
运行结果:
***************练习内部函数调用*****************
有一个参数的:
name :chenzhen
有多个参数的:
name :chenzhen
age :23
静态内部函数:
static function: statictest
**************练习私有变量调用*****************
name :huoyuanjia
**************练习私有构造函数***************
无参数的:
name :asdf
aget :0
有多个参数的:
name :zhou
aget :23
有一个参数的:
name :asdf
aget :23
Process finished with exit code 0