1.Student类
1 package com.zdsofe.javaweb.lianxi1; 2 3 public class Student { 4 public String stuName; 5 private String sex; 6 private int age; 7 8 public String getStuName() { 9 return stuName; 10 } 11 public void setStuName(String stuName) { 12 this.stuName = stuName; 13 } 14 public String getSex() { 15 return sex; 16 } 17 public void setSex(String sex) { 18 this.sex = sex; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 27 public Student(String stuName, int age) { 28 super(); 29 this.stuName = stuName; 30 this.age = age; 31 } 32 public Student(String stuName, String sex) { 33 super(); 34 this.stuName = stuName; 35 this.sex = sex; 36 } 37 public Student() { 38 39 } 40 public String tostring() 41 { 42 return "姓名为:"+this.stuName+",性别为:"+this.sex; 43 } 44 }
2.测试类
1 package com.zdsofe.javaweb.lianxi1; 2 3 4 import java.lang.reflect.Constructor; 5 import java.lang.reflect.Field; 6 import java.lang.reflect.Method; 7 8 public class Test { 9 10 public static void main(String[] args) throws Exception { 11 /*//获取class对象的方法一 12 Student stu=new Student(); 13 Class<?>class1=stu.getClass(); 14 System.out.println(class1); 15 16 //获取class对象的方法二(获得这个包下的这个类的地址) 17 Class<?> class2=Class.forName("com.zdsofe.javaweb.lianxi1.Student"); 18 System.out.println(class2); 19 20 //获取class对象的方法二 21 Class<?> class3=Student.class; 22 System.out.println(class3.getSimpleName()); 23 24 //获得类的public类型属性 25 Field[] pb=class3.getFields(); 26 System.out.println(pb.length); 27 28 //获得类的所有属性 29 Field[] pb1=class3.getDeclaredFields(); 30 System.out.println(pb1.length); 31 32 //获得类的public方法(继承自object类的方法也计算在内) 33 Method[]a=class3.getMethods(); 34 System.out.println(a.length); 35 for(Method i:a) 36 { 37 System.out.println(i); 38 } 39 40 //获得类的所有方法(当前类下的方法) 41 Method[]a1=class3.getDeclaredMethods(); 42 System.out.println(a1.length); 43 44 //获得类的public构造方法 45 Class<?> cla=Student.class; 46 Constructor<?>[] c=cla.getConstructors(); 47 System.out.println(c); 48 49 //获得所有的构造方法 50 Constructor<?>[] c1=cla.getDeclaredConstructors(); 51 System.out.println(c1); 52 */ 53 /* //获取Student类对应的class对象 54 Class<?> cls=Student.class; 55 56 //获取Student类的无参构造方法对应的Constructor对象 57 Constructor<?> c1=cls.getConstructor(); 58 59 //获取Student类的有参构造方法对应的Constructor对象 60 Constructor<?> c2=cls.getConstructor(String.class,String.class); 61 System.out.println(c2); 62 63 //调用Class对象的newInstance()方法创建Student对象 64 Student stu1=(Student)cls.newInstance(); 65 System.out.println(stu1.tostring()); 66 67 //调用Constructor对象的newInstance()方法创建Student对象(无参) 68 Student stu2=(Student)c1.newInstance(); 69 System.out.println(stu2.tostring()); 70 //调用Constructor对象的newInstance()方法创建Student对象(有参) 71 Student stu3=(Student)c2.newInstance("张三","女"); 72 System.out.println(stu3.tostring());*/ 73 74 75 /* //获取Student类对应的class对象 76 Class<?> cls=Student.class; 77 //通过Student的无参构造方法来创建Student对象 78 Object obj= cls.newInstance(); 79 //获取到stuName对应的Field对象 80 Field f1=cls.getField("stuName"); 81 //调用Field对象的set方法为对象的stuName属性赋值 82 f1.set(obj, "张三"); 83 //获取私有变量对应的Field的对象 84 Field f2=cls.getDeclaredField("sex"); 85 //sex是私有的,所以要通过反射机制访问该变量 86 f2.setAccessible(true); 87 f2.set(obj, "女"); 88 //获取到obj的属性值 89 Object result1= f2.get(obj); 90 Object result2=f1.get(obj); 91 System.out.println(result1+":"+result2); 92 Student stu=(Student)obj; 93 System.out.println(stu.getStuName()+":"+stu.getSex());*/ 94 95 //获取Student类对应的class对象 96 Class<?> cls=Student.class; 97 //通过方法名和方法的参数类型来获取Student类的public方法 98 Method m1= cls.getMethod("setStuName", String.class); 99 Method m2=cls.getMethod("setSex",String.class); 100 String name=m1.getName(); 101 System.out.println(name); 102 103 } 104 }