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
原创粉丝点击