15/7/28/class类和Field类的一般应用
来源:互联网 发布:网站源码小偷程序 编辑:程序博客网 时间:2024/06/05 18:01
class类和Field类的一般应用
public static void main(String[] args) { Student zhangsan = new Student(); Class<? extends Student> clazz = Student.class; try {//getDeclaredMethods返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,//包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。 Method[] methods = clazz.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { System.out.println(methods[i].getName());//getParameterTypes返回表示数组组件类型的 Class Class<?>[] parameter = methods[i].getParameterTypes(); for (int j = 0; j < parameter.length; j++) { System.out.println(parameter[j].getName()); } } Field field = clazz.getDeclaredField("age");//将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。// 值为 false 则指示反射的对象应该实施 Java 语言访问检查 field.setAccessible(true); field.setInt(zhangsan, 30);// 将字段的值设置为指定对象上的一个 int 值 field.setAccessible(false); System.out.println(zhangsan.getAge()); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } }public class Test01 { public static void main(String[] args) { Student student=new Student(); System.out.println(student.getAge()); Class<? extends Student> clazz=student.getClass(); try {//getDeclaredField返回一个 Field 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明字段 Field field=clazz.getDeclaredField("age");//将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。//值为 false 则指示反射的对象应该实施 Java 语言访问检查 field.setAccessible(true); field.setInt(student, 30);//将字段的值设置为指定对象上的一个 int 值 field.setAccessible(false); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(student.getAge());//返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段。 Field[] fields=clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { System.out.println(fields[i].getName()); } }}public class Student { private int age=20; public String name; public String sex; public int getAge() { return age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}
class类和Field类的一般应用
public class Clazz { @MyAnnotation(ageannotation=10,sexannotation="男") private Student zhangsan; @MyAnnotation(ageannotation=10,sexannotation="男",nameannotation="李四") private Student lisi; @MyAnnotation(ageannotation=10,sexannotation="男",nameannotation="王五") private Student wangwu; @NumAnnotation(numannotation=15) private Student zhaoliu; public Clazz(){ zhujie(); } public void zhujie(){ Class<? extends Clazz> cla=Clazz.class;// 返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段 Field[] fields=cla.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field=fields[i];//getAnnotation如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。 MyAnnotation annotation=field.getAnnotation(MyAnnotation.class); if(annotation!=null){ field.setAccessible(true); Student student=new Student(); student.setAge(annotation.ageannotation()); student.setName(annotation.nameannotation()); student.setSex(annotation.sexannotation()); try { field.set(this, student);//将Student的内容赋值给Clazz类中的对象的变量 field.setAccessible(false); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }else if(annotation==null){ NumAnnotation numannotation=field.getAnnotation(NumAnnotation.class); field.setAccessible(true); Student student1=new Student(); student1.setNum(numannotation.numannotation()); try { field.set(this, student1); field.setAccessible(false); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } public Student getZhangsan() { return zhangsan; } public void setZhangsan(Student zhangsan) { this.zhangsan = zhangsan; } public Student getLisi() { return lisi; } public void setLisi(Student lisi) { this.lisi = lisi; } public Student getWangwu() { return wangwu; } public void setWangwu(Student wangwu) { this.wangwu = wangwu; } public Student getZhaoliu() { return zhaoliu; } public void setZhaoliu(Student zhaoliu) { this.zhaoliu = zhaoliu; }}@Target(ElementType.FIELD)//表示用于成员变量和枚举常量@Retention(RetentionPolicy.RUNTIME)//表示在运行时加载Annotation到JVM中,有效范围大public @interface MyAnnotation { String nameannotation() default "张三";//默认变量为张三 String sexannotation(); int ageannotation();}@Target(ElementType.FIELD)//表示用于成员变量和枚举常量@Retention(RetentionPolicy.RUNTIME)//表示在运行时加载Annotation到JVM中,有效范围大public @interface NumAnnotation { int numannotation();}public class Student { private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } private int age; private String name; private String sex; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}public class Test { public static void main(String[] args) { Clazz clazz=new Clazz(); System.out.println(clazz.getZhangsan().getName()); System.out.println(clazz.getZhaoliu().getNum());}}
0 0
- 15/7/28/class类和Field类的一般应用
- Constructor,Field,Meothd类的反射应用
- 反射Class类、Constructor类、Field类
- 10、Class类的构造器:Constrctor、Class类的属性:field、Class类的方法:Method
- Java Class类加载资源的一般方法
- Constructor类和Field类的反射
- Field包含的类
- 黑马程序员-Class类, Constructor类,Field类, Method类的小总结
- 黑马程序员---反射,Class类,Method类,Field类,数组参数的成员方法
- JavaSE 第六十二讲: Class类、Method类及Field类的使用方式深度剖析
- 反射(二)Class类,Method类,Field类,反射获得带参数的对象
- Java学习--(四)新的数据类型:类(class);field,method
- fastjson:获取类(Class)的所有可序列化字段(field)
- JAVA进阶之旅(二)——认识Class类,反射的概念,Constructor,Field,Method,反射Main方法,数组的反射和实践
- 黑马程序员---反射(Class类、Constructor类、Field类)
- 关于Field类的getDeclaredFields和getField两方法
- android Field类的讲解
- Field类的构造方法
- 线段树_FZU_1921
- Java对象序列化和反序列化
- ASCⅡ码转换
- HDOJ 1896 Stones(优先队列)
- zoj3886.Nico Number
- 15/7/28/class类和Field类的一般应用
- linux上部署Tomcat服务器。
- Android用shareUserID实现多个Activity显示在同一界面
- 屏幕适配
- 算法--冒泡排序
- ORACLE-015:ora-25153 临时表空间为空,ora01652 无法通过128
- java基础之抽象方法
- 流程图的重要性
- 线索二叉树的遍历应用