需建立一个子类一个父类 包括共有私有属性值
Class<?> aClass = Class.forName("com.QQQ.People"); //获取本类中指定的属性对象 /* Field name = aClass.getDeclaredField("name"); System.out.println(name);*/ //获取本类和父类中指定的属性public对象 /*Field name1 = aClass.getField("qian"); System.out.println(name1);*/ //获取本类中所有属性对象 /*Field[] fields = aClass.getDeclaredFields(); for (Field field : fields) { System.out.println(field); }*/ //获取子类和父类的所有public属性对象 /*Field[] fields = aClass.getFields(); for (Field field : fields) { System.out.println(field); }*/
Class<People> aClass = People.class; //本类指定属性 Field name = aClass.getDeclaredField("name"); //获取属性名称 String name1 = name.getName(); System.out.println("name1========="+name1); People people = aClass.newInstance();//调用无参函数 name.setAccessible(true);//设置可访问权限 name.set(people,"王文超"); System.out.println(people); }
Class<People> aClass = People.class; //本类指定属性 Field name = aClass.getDeclaredField("name"); //获取属性名称 String name1 = name.getName(); System.out.println("name1========="+name1); People people = aClass.newInstance();//调用无参函数 name.setAccessible(true);//设置可访问权限 name.set(people,"王文超"); System.out.println(people); }
Class<Student> aClass = Student.class; //获取类对象的指定注解 MyAnnotion annotation = aClass.getAnnotation(MyAnnotion.class); System.out.println(annotation.value()); Field id = aClass.getDeclaredField("id"); MyAnnotion annotatedType = id.getAnnotation(MyAnnotion.class); System.out.println(annotatedType.value()); System.out.println(annotatedType.sex()); }
@Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotion { String value(); String sex() default "男"; }
@MyAnnotion(value = "tbl_user") public class Student { @MyAnnotion(value = "sid",sex="幺") private int id; @MyAnnotion(value = "t_show") public void show(){ } }