java 注解

定义

1、如果注解中有属性,那么必须给属性赋值。

package com.lxc.Test;

// 定义一个注解
public @interface Annotation {
    String name(); // 看似name像一个方法,实际上我们把name称为属性
}

 使用上边注解:

package com.lxc.Test;

public class Test {
    @Annotation(name="lxc")
    public void test() {
    }
}

2、如果注解中有属性,且没有定义默认值,那么在使用注解的时候,必须给属性赋值。

public @interface Annotation {
    String name();
    int age();
}
public class Test {
    @Annotation(name="lxc", age=20)
    public void test() {
    }
}

但是注解中如果有默认值,在使用注解时,可以不给属性赋值

public class Test {
    @Annotation(name="lxc")
    public void test() {
    }
}
public @interface Annotation {
    String name();
    String password() default "123";
}

3、value() 属性

如果注解中的一个属性名是value,且有且只有一个value(),在使用注解的时候,属性名可以省略

public class Test {
    @Annotation("lxc")
    public void test() {
    }
}
public @interface Annotation {
    String value();
    String password() default "123";
}

注解中属性的类型有哪些

byte、short、int、float、double、boolean、char、String、Class、枚举

1

数组:
 如果数组属性中有一个元素,那么数组的大括号可以省略:

public @interface Annotation {
    String[] name();
}
public class Test {
    // @Annotation(name={"lxc"}) // 写法一
    @Annotation(name="lxc") // 写法二
    public void test() {
    }
}

枚举:

public enum MyEnum {
    className, name, age,
}
public @interface Annotation {
    String[] name();
    MyEnum[] student();
}
public class Test {
    @Annotation(name="lxc", student = {MyEnum.className, MyEnum.age})
    public void test() {
    }
}

注解如何使用:

(1)@Target

标记一个注解只能出现在类或者方法上

元注解,用来标注注解类型的注解。

@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface Annotation {
    String userName();
    MyEnum[] student();
}

(2)@Retention

@Retention(RetentionPolicy.RUNTIME) 标记一个注解可以被反射机制所读取
@Retention(RetentionPolicy.SOURCE) 标记一个注解只能被保留在java源文件中
如:@Override 注解只是给编译器看的,所以只存在在java文件中,编译之后就没有了。

@Retention(RetentionPolicy.CLASS) 标记一个注解可以被保存在class文件中 

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

// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
    String userName();
    MyEnum[] student();
}

获取注解中的属性值

通过反射机制来获取。

(1)获取类上边注解的属性:

注解类:

package com.lxc.Test;

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

// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
    String userName() default "吕星辰";
}

使用注解类:

// myAnnotation 
@Annotation(userName = "哈哈")
public class myAnnotation {
}

获取注解类中 的属性:

package com.lxc.Test;
/**
 * c.isAnnotationPresent(注解类.class) : 判断一个类上是否有注解,返回true、false
 * c.getAnnotation(注解类.class) : 获取注解类的实例
 *
 */
public class Test {
    public static void main(String[] args) throws Exception{
        Class c = Class.forName("com.lxc.Test.myAnnotation");
        System.out.println(c.isAnnotationPresent(Annotation.class));
        // 判断一个类是否有:Annotation这个注解
        if(c.isAnnotationPresent(Annotation.class)) {
            // 获取注解对象
            Annotation ann = (Annotation) c.getAnnotation(Annotation.class);
            // 通过注解对象获取属性值
            System.out.println(ann.userName());
        }
    }
}

(2)获取类中方法上边注解的属性:

注解类:

package com.lxc.Test;

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

// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
    String userName();
    String password();
}

在方法上使用注解及获取方法上边的注解:
分析:想获取方法上的注解,首先需要获取该方法,获取该方法的前提,获取该方法的类:

package com.lxc.Test;

import java.lang.reflect.Method;

public class UserAnnotation {
    @Annotation(userName = "lxc", password = "123")
    public void getUser() {}

    public static void main(String[] args) throws Exception{
        // 通过反射获取类
        Class c = Class.forName("com.lxc.Test.UserAnnotation");
        // 通过反射获取类中的方法
        Method getUserMethod = c.getDeclaredMethod("getUser");
        // 判断方法是否有 Annotation 这个注解
        if(getUserMethod.isAnnotationPresent(Annotation.class)) {
            Annotation ann = getUserMethod.getAnnotation(Annotation.class);
            System.out.println(ann.userName()); // lxc
            System.out.println(ann.password()); // 123
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值