您的位置:首页 > 编程语言 > Java开发

Java反射的使用

2016-10-11 18:53 120 查看
主类:

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* Created by cuboo on 2016/10/11.
*/
public class Main01 {
public static void main(String agrs[]){
Main01 mains = new Main01();
exam01 exam = new exam01();
//获得exam01的对象 方法1  方法2 examC = Class.forName(类路径) 方法3 examC = exam01.class
Class examC = exam.getClass();
//        mains.constructor(examC);
//        mains.field(examC,exam);
mains.method(examC,exam);
}
private void constructor(Class examC){
//获得所有构造器方法
Constructor[] constructors= examC.getDeclaredConstructors();
//遍历构造方法
for (int i = 0; i < constructors.length ; i++) {
Constructor constrctor = constructors[i];
System.out.println("是否带有可变数量的参数:"+constrctor.isVarArgs());
System.out.println("入口参数类型依次是:");
Class[] parameter = constrctor.getParameterTypes();
for (int j = 0; j < parameter.length; j++) {
System.out.println(" "+parameter[j]);
}
System.out.println("可能抛出的异常值:");
Class[] exception = constrctor.getExceptionTypes();
for (int j = 0; j < exception.length; j++) {
System.out.println(" "+exception[j]);
}

exam01 exam0 = null;
while (exam0 == null){
//如果为privatex对象则抛出异常
try {
if (i == 2){
exam0 = (exam01) constrctor.newInstance();
}else if (i == 1){
exam0 = (exam01) constrctor.newInstance("2个参数的构造方法",2);
}else {
Object[] parameters = new Object[]{new String[]{"1","2","3"}};
exam0 = (exam01) constrctor.newInstance(parameters);
}

}catch (Exception e){
System.out.println("创建对象时抛出异常,下面执行setAccessible()方法");
//设置允许访问
constrctor.setAccessible(true);
}

}
exam0.print();
}
}
private void field(Class examC,exam01 exam){
//获得所有成员变量
Field[] fields = examC.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
//获得成员变量名称
System.out.println("名称:"+field.getName());
System.out.println("类型:"+field.getType());
boolean isTurn = true;
while(isTurn){
//如果为private则抛出异常
try{
isTurn = false;
System.out.println("修改前的值:"+field.get(exam));
if (field.getType().equals(int.class)){
field.setInt(exam,100);
}else if (field.getType().equals(float.class)){
field.setFloat(exam,99.99f);
}else {
//设置各种类型的成员变量的值
field.set(exam,"成员变量"+field.getName());
}
System.out.println("修改后的值:"+field.get(exam));
}catch (Exception e){
System.out.println("设置成员变量是出现异常,执行setAccessible方法");
field.setAccessible(true);
isTurn = true;
}
}
}
}
private  void method(Class examC,exam01 exam){
//获得所有方法
Method[] methods = examC.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
System.out.println("\n方法名称:"+method.getName());
System.out.println("是否可变参数:"+method.isVarArgs());
System.out.println("入口参数依次为:");
Class[] parameters = method.getParameterTypes();
if (parameters.length == 0){
System.out.println("null");
}
for (int j = 0; j < parameters.length; j++) {
System.out.println(parameters[j]);
}
//获得方法返回值的类型
System.out.println("返回值类型:"+method.getReturnType());

boolean isTurn = true;
while(isTurn){
//如果为private则抛出异常
try{
isTurn = false;

if ("staticMethod".equals(method.getName())){
System.out.println(method.invoke(exam));
}else if ("privateMethod".equals(method.getName())){
System.out.println(method.invoke(exam,100));
}else if ("protectedMethod".equals(method.getName())){
System.out.println(method.invoke(exam,"string",100));
}else if ("publicMethod".equals(method.getName())){
Object[] paras = new Object[]{new String[]{"a","b","c"}};
System.out.println(method.invoke(exam,paras));
}

}catch (Exception e){
System.out.println("设置成员方法是出现异常,执行setAccessible方法");
method.setAccessible(true);
isTurn = true;
}
}
}
}
}
调用类:
/**
* Created by cuboo on 2016/10/10.
*/
public class exam01 {
String s;
int i,i2,i3;
private String str;
protected int num;
public float nums;
private exam01(){

}
protected exam01(String s,int i){
this.s = s;
this.i = i;
}
public exam01(String... strings) throws NumberFormatException{
if (strings.length > 0){
i = Integer.valueOf(strings[0]);
}
if (strings.length > 1){
i2 = Integer.valueOf(strings[1]);
}
if (strings.length > 2){
i3 = Integer.valueOf(strings[2]);
}
}
public void print(){
System.out.println("s="+s);
System.out.println("i="+i);
System.out.println("i2="+i2);
System.out.println("i3="+i3);
}
static void staticMethod(){
System.out.println("执行staticMethod方法");
}
private void privateMethod(int i){
System.out.println("执行privateMethod方法"+i);
}
protected void protectedMethod(String s,int i){
System.out.println("执行protectedMethod方法"+s+" "+i);
}
public String publicMethod(String... strings){
System.out.println("执行publicMethod方法");
for (int j = 0; j < strings.length; j++) {
System.out.print(strings[j]+" ");
}
System.out.println();
return null;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: