反射的例子

来源:互联网 发布:virtualbox安装mac os 编辑:程序博客网 时间:2024/06/09 17:32
import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class ReflectionExample {public static void main(String[] args) {try {// Creates an object of type Class which contains the information of// the class StringClass cl = Class.forName("java.lang.String");// getDeclaredFields() returns all the constructors of the class.Constructor cnst[] = cl.getConstructors();// getFields() returns all the declared fields of the class.Field fld[] = cl.getDeclaredFields();// getMethods() returns all the declared methods of the class.Method mtd[] = cl.getMethods();System.out.println("Name of the Constructors of the String class");for (int i = 0; i < cnst.length; i++) {System.out.println(cnst[i].getName());}System.out.println("Name of the Declared fields");for (int i = 0; i < fld.length; i++) {System.out.println(fld[i].getName());}System.out.println("Name of the Methods");for (int i = 0; i < mtd.length; i++) {System.out.println(mtd[i].getName());}} catch (ClassNotFoundException e) {e.printStackTrace();}}}
查看Rectangle类的属性,方法:http://188029.net/java/j120615.html
原创粉丝点击