java 反射机制 java 反射机制详解及实例代码
人气:0想了解java 反射机制详解及实例代码的相关内容吗,在本文为您仔细讲解java 反射机制的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,反射机制,java,反射机制实例,反射机制,下面大家一起来学习吧。
java 反射机制:
测试实体类
以Human为例
/** * Project: Day12_for_lxy * Created: Lulu * Date: 2016/8/10 */ public class Human<T> { private String name; private int age; private float height; private static int legs = 2; private Map<String, String> jobs; private T t; public int say() { System.out.println("I'm" + name); return 0; } private void sleep(Human human) { System.out.println(name + " sleep with " + human.name); } public List<Integer> getList() { return new ArrayList<>(); } public Human() { } private Human(String name, int age, float height) { this.name = name; this.age = age; this.height = height; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if (age > 150 || age < 0) { throw new RuntimeException("age > 150 || age < 0"); } this.age = age; } public float getHeight() { return height; } public void setHeight(float height) { this.height = height; } @Override public String toString() { return "Human{" + "name='" + name + '\'' + ", age=" + age + ", height=" + height + '}'; } }
测试获取构造方法
/** * Project: Day12_for_lxy * Created: Lulu * Date: 2016/8/10 */ /* 获取构造方法 */ public class TestConstructor { public static void main(String[] args) { //得到相应的类结构, 这是用来描述Human这个类的类 Class<Human> humanClass = Human.class; try { //// 这是在知道构造方法结构的前提下 // //获取都明确参数的构造方法, 获取的是公开的(public) // Constructor<Human> constructor1 = humanClass.getConstructor(String.class, Integer.TYPE, Float.TYPE); // // Human h1 = constructor1.newInstance("小明", 18, 1.85f); // System.out.println(h1.toString()); // // //构造方法, 修饰符类型 // int modifiers = constructor1.getModifiers(); // if (Modifier.isPublic(modifiers)) { // System.out.println("是public"); // } // ////获取都明确参数的构造方法, 获取的是声明的 // Constructor<Human> constructor2 = humanClass.getDeclaredConstructor(String.class, Integer.TYPE, Float.TYPE); // //设置可以全部访问 // constructor2.setAccessible(true); // //这样是可以获取到所有的构造方法, 包括私有的 // Human human2 = constructor2.newInstance("zll", 18, 1.80f); // System.out.println(human2.toString()); //不知道构造方法的类结构 Constructor<?>[] constructors = humanClass.getDeclaredConstructors(); for (Constructor<?> c : constructors) { c.setAccessible(true); System.out.println(c.getName()); System.out.println("==========================="); //参数列表 Class<?>[] types = c.getParameterTypes(); for (Class<?> type : types) { System.out.println(type.getTypeName()); } //修饰符类型 int modifiers = c.getModifiers(); if (Modifier.isPublic(modifiers)) { System.out.println("是公开的"); }else if (Modifier.isPrivate(modifiers)){ System.out.println("是私有的"); } } } catch (Exception e) { e.printStackTrace(); } } }
测试获取字段
/** * Project: Day12_for_lxy * Created: Lulu * Date: 2016/8/10 */ /* 获取属性: 属性包括:( 属性名 类型 修饰符 泛型 ) 父类的属性 自身的属性 */ public class TestField { public static void main(String[] args) { Class<Human> humanClass = Human.class; Field[] fields = humanClass.getDeclaredFields(); for (Field f : fields) { //属性名 System.out.print(" 名字: " + f.getName() + " "); System.out.println(); //类型 System.out.print("类型 :" + f.getType() + " "); System.out.println(); //修饰符 int modifiers = f.getModifiers(); if (Modifier.isPublic(modifiers)) { System.out.println("公开的"); } else if (Modifier.isPrivate(modifiers)) { System.out.println("私有的"); } } System.out.println("============================泛型=================================="); try { //通过类结构获取jobs属性 Field jobs = humanClass.getDeclaredField("jobs"); //泛型 ParameterizedType type = (ParameterizedType) jobs.getAnnotatedType().getType(); Type[] types = type.getActualTypeArguments(); for (Type type1 : types) { System.out.println(type1); } } catch (Exception e) { e.printStackTrace(); } System.out.println("===================设置值, 得到值============================"); try { Human<Object> human = new Human<>(); //非静态, 你要知道给谁设置属性的值 Field nameField = humanClass.getDeclaredField("name"); nameField.setAccessible(true); nameField.set(human, "路新艺"); System.out.println(human.getName()); //静态 Field legs = humanClass.getDeclaredField("legs"); int modifiers = legs.getModifiers(); legs.setAccessible(true); if (Modifier.isStatic(modifiers)) { System.out.println("是静态的"); } legs.set(null, 4); System.out.println(legs.get(null)); } catch (Exception e) { e.printStackTrace(); } } }
测试获取方法
/** * Project: Day12_for_lxy * Created: Lulu * Date: 2016/8/10 */ /* 获取方法: 方法名 修饰符 返回值类型 泛型 静态和非静态 方法的调用 */ public class TestMethod { public static void main(String[] args) { Class<Human> humanClass = Human.class; //方法名 Method[] ms = humanClass.getDeclaredMethods(); for (Method m : ms) { m.setAccessible(true); System.out.print("方法名: " + m.getName() + " " ); int modifiers = m.getModifiers(); if (Modifier.isPublic(modifiers)) { System.out.println("公开的"); } else if (Modifier.isPrivate(modifiers)) { System.out.println("私有的"); } } //在确定方法参数的情况下 方法名不能唯一的确定, 重载 try { //普通类型 Method sayM = humanClass.getDeclaredMethod("say"); Class<?> returnType = sayM.getReturnType(); System.out.println(returnType.toString()); //泛型 Method getListM = humanClass.getDeclaredMethod("getList"); System.out.println(getListM.getReturnType()); ParameterizedType type = (ParameterizedType) getListM.getAnnotatedReturnType().getType(); System.out.println(type); Type[] ts = type.getActualTypeArguments(); for (Type t : ts) { System.out.println(t); } } catch (Exception e) { e.printStackTrace(); } } }
测试获取类信息
public static void main(String[] args) { printClassInfo(Student.class); } public static void printClassInfo(Class c){ //Java中唯一的父类的类时Object System.out.println(c.getName()); System.out.println(c.getSimpleName()); System.out.println(c.getSuperclass()); Class[] interfaces = c.getInterfaces(); for (Class anInterface : interfaces) { System.out.println(anInterface); } //外部类类只有两种访问权限修饰(public 和 default) int modifiers = c.getModifiers(); }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
加载全部内容