javaSE-反射

来源:互联网 发布:淘宝新店一单刷6个 编辑:程序博客网 时间:2024/05/16 05:17





public class Person {private String name;private int age;public Person() {super();System.out.println("person run");}public Person(String name, int age) {super();this.name = name;this.age = age;System.out.println(this.name+":::"+this.age);}/* * 公有的空参数方法。 */public void show(){System.out.println("Person show run");}/* * 公有,带参数的方法。 *  */public void show2(String str,int num){System.out.println(str+":::"+num);}/* * 获取静态方法。 */public static void show3(){System.out.println("Person static show3 run");}/* * 练习: */private void show4(){System.out.println(this.name+"::::::"+this.age);//wangcai:::::20}}

public class ReflectDemo {/** * @param args * @throws ClassNotFoundException  */public static void main(String[] args) throws ClassNotFoundException {/* *  * 反射机制:对一个类进行动态的获取并创建对象,并调用该类中的内容。 *  * 获取字节码文件对象的方式。 *  * 方式一: * 使用Object类中的getClass方法。 * 该方式必须先创建未知类的对象,在调用getClass方法。 *  * 方式二: * 使用静态的class属性就可以获取。 * 该方式还是需要使用未知的类。 *  * 方式三: * 通过Class类中的静态方法 forName(className)完成; * 爽!只要通过类名就可以获取到对应的字节码文件对象。 * 这种方式的扩展性是最好的。 *  */getClassDemo_3();}//方式三public static void getClassDemo_3() throws ClassNotFoundException{String className = "cn.itcast.bean.Person";/* * forName: * 1,根据传入的类的名称,自动去classPath路径下去找寻与该名称相同的类文件。 * 2,通过类加载器将该类进行加载,并在内存中生成该类的字节码文件对象。 */Class clazz = Class.forName(className);System.out.println(clazz);}//方式二。public static void getClassDemo_2() {Class clazz = Person.class;Class clazz1 = int.class;}//方式一。public static void getClassDemo_1(){Person p1 = new Person();Person p2 = new Person();Class clazz1 = p1.getClass();Class clazz2 = p2.getClass();System.out.println(clazz1 == clazz2);}}

反射字段

public static void getFieldDemo() throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException{String className = "Person";/* * 获取指定类中的指定名称的字段。 * 思路: * 1,通过类名获取字节码文件对象。 * 2,通过字节码文件对象获取其中的成员字段。 *///1,获取字节码文件对象。Class clazz = Class.forName(className);//2,通过Class对象的方法获取字段。//Field field = clazz.getField("age");//该方法只能访问公有的。Field field = clazz.getDeclaredField("age");//可以访问已声明的字段。包括私有。//System.out.println(field);//Person p = new Person();//p.age = 20;//System.out.println(p.age);//要想对指定属性进行值的设置。必须要先有指定类的对象。//对象如何创建呢?没法儿new啊?对象都来自于字节码文件。已经有了字节码文件,获取其对象还远吗?不远!Object obj = clazz.newInstance();//取消权限检查。field.setAccessible(true);//暴力访问。//对字段进行值的设置,需要使用字段对象来完成。field.set(obj, 30);System.out.println(field.get(obj));//System.out.println(clazz.getName());}

反射构造函数

public static void getConstructorDemo() throws ClassNotFoundException, NoSuchMethodException, Exception{String className = "Person";Class clazz = Class.forName(className);//之前是通过newInstance进行指定的类的实例化。但是该类中如果没有空参数构造函数呢?//或者想通过指定的构造函数进行该类的对象实例化呢?咋办?/* * 1,必须先要获取指定的构造函数。 * 2,通过获取到的构造函数对对象进行初始化。 *///Person p = new Person("lisi",20);//1,如何获取构造函数呢?通过Class对象。Constructor constructor = clazz.getConstructor(String.class,int.class);//2,如何初始化,构造器最清楚。Object obj = constructor.newInstance("lisi",20);}

反射方法

public static void main(String[] args) throws Exception {getMethodDemo3();}/* * 获取公有静态的空参数函数。 */public static void getMethodDemo3() throws Exception{String className = "Person";Class clazz = Class.forName(className);Method method = clazz.getMethod("show3", null);method.invoke(null, null);}/* * 获取公有,带参数的函数。 */public static void getMethodDemo2() throws Exception{String className = "Person";Class clazz = Class.forName(className);Method method = clazz.getMethod("show2", String.class,int.class);Object obj = clazz.newInstance();method.invoke(obj, "wangwu",32);}/* * 获取方法。获取公有,空参数的函数。 */public static void getMethodDemo() throws Exception{String className = "Person";Class clazz = Class.forName(className);//获取指定类中的指定方法。Method method = clazz.getMethod("show", null);//System.out.println(method);/* * 让方法运行起来。 *///Person p = new Person();//p.show();//获取指定类的对象。Object obj = clazz.newInstance();//调用方法对象的invoke方法。method.invoke(obj,null);}



0 0