Java学习之路0728(十二)(Class类、反射、Annotation功能注解)

Class类、反射

public class Student {
    private int age=20;
    public String name;
    public String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String string) {
        this.sex = string;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        // TODO Auto-generated method stub
        return age;
    }    
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ClassTest {
    public static void main(String[] args) {
        Student zhangsan=new Student();
        System.out.println(zhangsan.getAge());
        Class<? extends Student> clasz=zhangsan.getClass();
        try {
            Method[] method=clasz.getDeclaredMethods();
            for (int i = 0; i < method.length; i++) {
                System.out.println("方法:"+method[i].getName());
                Class[] Paraments=method[i].getParameterTypes();
                for (int j = 0; j < Paraments.length; j++) {
                    System.out.println("参数:"+Paraments[j].getName());
                }
            }
            Field field=clasz.getDeclaredField("age");
            field.setAccessible(true);
            field.set(zhangsan, 30);
            System.out.println(zhangsan.getAge());
            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();
        }
        try {
            System.out.println(clasz.getDeclaredField("age").getName());
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Field[] field=clasz.getFields();
        for (int i = 0; i < field.length; i++) {
            System.out.println(field[i].getName());
        }

    }
}

运行结果:
20
方法:getName
方法:setName
参数:java.lang.String
方法:getAge
方法:getSex
方法:setSex
参数:java.lang.String
方法:setAge
参数:int
30
age
name
sex

Annotation功能注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.lang.model.element.Element;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    //这是注解
    String nameAnnotation()  default "张三";
    String sexAnntation();
    int ageAnntation();
}
public class Student {
    private int age=20;
    public String name;
    public String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String string) {
        this.sex = string;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        // TODO Auto-generated method stub
        return age;
    }


}
public class Clasz {
    @MyAnnotation(ageAnntation=20, sexAnntation = "男")
    public Student zhangsan;
    @MyAnnotation(ageAnntation=20, sexAnntation = "男",nameAnnotation="李四")
    public Student lisi;
    @MyAnnotation(ageAnntation=20, sexAnntation = "男",nameAnnotation="王五")
    public Student wangwu;
    public Student zhaoliu;
    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;
    }

}
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class MyAnntationTest {
    public static void main(String[] args) {
        Clasz clasz=new Clasz();
        Class<? extends Clasz> cla=clasz.getClass();
        Field[] fields=cla.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field=fields[i];
            MyAnnotation anntation=field.getAnnotation(MyAnnotation.class);
            if (anntation!=null) {              
                try {
                    field.setAccessible(true);
                    Student student=new Student();
                    student.setAge(anntation.ageAnntation());
                    student.setName(anntation.nameAnnotation());
                    student.setSex(anntation.sexAnntation());
                    field.set(clasz, student);
                    field.setAccessible(false);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
            }
        }
        System.out.println(clasz.zhangsan.getName());
    }
}

运行结果:张三

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值