反射--解析信息

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

/**
*
*/
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
@SuppressWarnings("unchecked")
public class TestReflection01 {
public static void main(String[] args) {
System.out.println("请输入类型名称:");
Scanner scan = new Scanner(System.in);
String className = scan.next();
System.out.println("当前类型:" + className);

try {
//获取Class对象
Class c = Class.forName(className);

//解析属性信息
Field[] fs = c.getDeclaredFields();
for (Field f : fs) {
System.out.println("----------------------------------------------");
System.out.println("属性:" + f.toString());
System.out.println("\t数据类型:" + f.getType());
System.out.println("\t属性名:" + f.getName());
int mod = f.getModifiers();
System.out.println("\t属性修饰符:" + Modifier.toString(mod));
}

//解析方法信息
Method[] ms = c.getDeclaredMethods();
for(Method m : ms){
System.out.println("----------------------------------------------");
System.out.println("方法:" + m.toString());
System.out.println("\t方法名:" + m.getName());
int mod = m.getModifiers();
System.out.println("\t方法修饰符:" + Modifier.toString(mod));
System.out.print("\t方法参数列表:");
Class pts[] = m.getParameterTypes();
for (int i = 0; i < pts.length; i++) {
Class class1 = pts[i];
if(i != 0 )
System.out.print(", ");
System.out.print(class1);
}
System.out.println();
System.out.println("\t返回值类型:" + m.getReturnType());
}

//解析构造方法信息
Constructor[] cs = c.getDeclaredConstructors();
for (Constructor con : cs) {
System.out.println("----------------------------------------------");
System.out.println("构造方法:" + con.toString());
System.out.println("\t方法名:" + con.getName());
int mod = con.getModifiers();
System.out.println("\t方法修饰符:" + Modifier.toString(mod));
System.out.print("\t方法参数列表:");
Class pts[] = con.getParameterTypes();
for (int i = 0; i < pts.length; i++) {
Class class1 = pts[i];
if(i != 0 )
System.out.print(", ");
System.out.print(class1);
}
System.out.println();
}
System.out.println("----------------------------------------------");

//解析当前类类型的父类
Class superClass = c.getSuperclass();
System.out.println("当前类的父类:" + superClass.toString());
System.out.println("----------------------------------------------");

//解析当前类实现的接口
Class[] interfaces = c.getInterfaces();
System.out.print("当前类所实现接口:");
for (Class class1 : interfaces) {
System.out.print(class1.toString() + "\t");
}
System.out.println("\n----------------------------------------------");

//解析当前类型所在包信息
Package p = c.getPackage();
System.out.println("当前类所在包" + p.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

}

你可能感兴趣的:(C++,c,C#,F#)