反射基础—实例

来源:互联网 发布:asp json解析 编辑:程序博客网 时间:2024/06/05 22:54
---------------------- android培训、java培训、期待与您交流! ----------------------
package com.darkhorse.admissiontest;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;a

实例一:
     //反射

public class Person {      public static void main(String[] args) throws Exception {//          Constructor<?> person = Person.class.getConstructor(String.class, int.class);//          Person p = (Person) person.newInstance("name", 49);                    // 从类加载器中获取Person类          Class<?> c = Class.forName(Person.class.getName());          // 获取带两个参数的构造函数          Constructor<?> cc = c.getConstructor(String.class, int.class);          // 调用构造函数实例化对象          Person p1 = (Person) cc.newInstance("vvvv", 22);                    //给对象p1的setName方法赋值pppppppp          p1.setName("pppppppp");                    // 获取setName方法          Method setName = c.getMethod("setName", String.class);                    // 获取私有的成员方法          // setName = c.getDeclaredMethod("setName", String.class);                    // 调用,其中p1代表是执行哪个对象中的setName方法          setName.invoke(p1, "abcccc");                    // 获取公有的成员变量age//          Field age = c.getField("age");//          int a = age.getInt(p1);                    // 获取私有的成员变量age          Field age = c.getDeclaredField("age");          int a = age.getInt(p1);                    System.out.println(a);          System.out.println(p1.getName());               }     private String name;     private int age;     public Person(String name, int age) {          this.name = name;          this.age = age;     }          public String getName() {          return name;     }     public void setName(String name) {          this.name = name;     }     public int getAge() {          return age;     }     public void setAge(int age) {          this.age = age;     }     }


--------------------------------------------------------------------------------------------------------------------------------------------------
实例二:
package com.darkhorse.admissiontest;import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class Student {     /**     * @param args     *            /* 定义一个标准的JavaBean     *            Student,有name、age、tel、3个成员变量,通过反射的方式实例化该Student对象     *            ,并同时初始化name,age两个成员变量,     *            再通过反射setTel方法设置tel成员变量的值为13800138000,最后通过反射调用getName     *            ,getAge,getTel这3个方法把对象的值输出。     */     /* 定义三个成员变量name,age,tel */     private String name;     private int age;     private long tel;     /* 定义一个构造函数 */     public Student(String name, int age) {          this.name = name;          this.age = age;     }     public String getName() {          return name;     }     public void setName(String name) {          this.name = name;     }     public int getAge() {          return age;     }     public void setAge(int age) {          this.age = age;     }     public long getTel() {          return tel;     }     public void setTel(long tel) {          this.tel = tel;     }     /* 使用主函数通过反射 */     public static void main(String[] args) {          try {               /*     前三行定义student的反射     */               Class<?> student = Class.forName(Student.class.getName());     //用加载器获取student类               Constructor<?> student1 = student.getConstructor(String.class, int.class);          //获取带两个元素的构造函数               Student stu = (Student) student1.newInstance("张三", 30);               //调用构造函数实例化对象                             /*     获取公有的方法,setTel()     */               Method setTel = student.getMethod("setTel", long.class);               /*     调用方法并给赋值,其中p1代表是执行哪个对象中的setName方法     */               setTel.invoke(stu, 1380013800);               /*     直接输出来     */               System.out.println(stu.getName());               System.out.println(stu.getAge());               System.out.println(stu.getTel());               /*     通过反射把它们的值输出     */               //获取公有的方法getName,没有元素后面不用加*.class               Method getName = student.getMethod("getName");               //调用方法,stu是对应的实例对象               System.out.println(getName.invoke(stu));               Method getAge = student.getMethod("getAge");               System.out.println(getAge.invoke(stu));               Method getTel = student.getMethod("getTel");               System.out.println(getTel.invoke(stu));          } catch (Exception e) {               // TODO Auto-generated catch block               e.printStackTrace();          }     }}





---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima