java基础-注解&反射

内置注解

@Override
@SuppressWarning

元注解:负责注解其他注解

@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
@Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(source<class<runtime)
@Document:说明该注解将被包含在javadoc中
@Inherited:说明子类可以继承父类中的该注解
public class Test1 {
    @MyAnnotation
    public void test(){

    }
}
// 元注解:target:目标作用位置
@Target(value = ElementType.METHOD)
// 元注解 :retention 有效周期
@Retention(value = RetentionPolicy.RUNTIME)
// 元注解: documented 是否生成在javadoc 中
@Documented
// 元注解: inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation{

}

自定义注解

public class Test2 {
    @MyAnnotation2(name = "秦疆")
    public void test(){};

    // 当只有一个参数时,规范是以value为参数名
    @MyAnnotation3(value = "normal")
    public void test2(){};
}

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    // 注解的参数:参数类型+参数名 ();
    String name() default "";
}


@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    String value();
}

反射

public class Test4 {
    public static void main(String[] args) throws ClassNotFoundException {
        Person student = new Student();
        System.out.println("此人是"+student.name);

        // 方式一: 通过对象获得
        Class<? extends Person> c1 = student.getClass();
        System.out.println(c1.hashCode());
        // 方式二: forname获得
        Class<?> c2 = Class.forName("com.oscar.reflection.Student");
        System.out.println(c2.hashCode());
        // 通过类名.class 获得
        Class<Student> c3 = Student.class;
        System.out.println(c3.hashCode());

        // 方式四:基本内置类型的包装类>> Type属性
        Class<Integer> c4 = Integer.TYPE;
        System.out.println(c4);

        //方式五:获取父类类型
        Class<?> c1Superclass = c1.getSuperclass();
        System.out.println(c1Superclass.hashCode()  );

    }

}
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("com.oscar.reflection.User");


        // 获得带有包名的类名
        System.out.println(c1.getName());
        // 获得不带有包名的类名
        System.out.println(c1.getSimpleName());
        // getFields 方法只能找到 public修饰的属性
        Field[] fields = c1.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        // getDeclaredFields 方法可以找到全部的属性
        fields = c1.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        // getDeclaredField 方法同getDeclaredFields
        Field name = c1.getDeclaredField("name");
        System.out.println(name);

        // getField 方法同getFields
        Field name1 = c1.getField("name");
        System.out.println(name);

        // 获得public修饰的方法
        Method[] methods = c1.getMethods();
        // 获得全部的方法
        Method[] declaredMethods = c1.getDeclaredMethods();

        // 获得全部的构造器
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        // 获得public修饰的构造器
        Constructor[] constructors = c1.getConstructors();
    }

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class c1 = Class.forName("com.oscar.reflection.Human");
        // 通过反射获得c1 Class对象> newInstance创建对象
        Human user = (Human) c1.newInstance();
        System.out.println(user);

        // 通过构造器创建对象
        Constructor<?> declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
        Human worker = (Human) declaredConstructor.newInstance("王凡", 1, 24);
        System.out.println(worker);

        // 通过反射调用普通方法
        Human form = (Human) c1.newInstance();
        Method setName = c1.getDeclaredMethod("setName", String.class);
        setName.invoke(form,"邓聪");
        System.out.println(form.getName());

        // 通过反射操作属性
        Human user2 = (Human) c1.newInstance();
        Field field = c1.getDeclaredField("name");
        field.set(user2,"司梦涛");
        System.out.println(user2.getName());

    }

注解*反射

public class Test8 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> student = Class.forName("com.oscar.annotation.demo1.Student");

        // 通过反射获得注解 getAnnotations;
        Annotation[] annotations = student.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        // 获得 注解的value值 *
        System.out.println(student.getAnnotation(TestInterface.class).value());

        // 获得类指定(?)的注解
        Field name = student.getDeclaredField("name");
        Annotation[] nameAnnotations = name.getAnnotations();
        for (Annotation nameAnnotation : nameAnnotations) {
            System.out.println(nameAnnotation);
        }
        TestInterfaceField annotation = name.getAnnotation(TestInterfaceField.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());

    }
}
@TestInterface("db_student")
class Student{
    @TestInterfaceField(columnName = "db_id",type = "int",length = 20)
    String name;
    int age;
    int id;

    public Student() {
    }

    public Student(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TestInterface{
    String value();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface TestInterfaceField{
    String columnName();
    String type() default "Varchar";
    int length() default 255;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值