java教程——注解(二)

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
Constructor.isAnnotationPresent(Class)

package test;

import java.lang.annotation.*;

public class changeData {

public static void main(String[] args) {

Class pClass = persson.class;

System.out.println(pClass.isAnnotationPresent(selfAnnotation.class));

}

}

@selfAnnotation

class persson{

private int num;

public int getNum(){

return this.num;

}

}

//第三步:添加元注解

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

//第一步:定义注解

@interface selfAnnotation{

//第二步:定义参数

int type() default 1;

}

二、使用反射API读取Annotation:

Class.getAnnotation(Class)

Field.getAnnotation(Class)

Method.getAnnotation(Class)

Constructor.getAnnotation(Class)

package test;

import java.lang.annotation.*;

public class changeData {

public static void main(String[] args) {

Class pClass = persson.class;

if(pClass.isAnnotationPresent(selfAnnotation.class)){

selfAnnotation annotation = pClass.getAnnotation(selfAnnotation.class);

System.out.println(annotation.type());

}

}

}

@selfAnnotation(type = 2)

class persson{

private int num;

public int getNum(){

return this.num;

}

}

//第三步:添加元注解

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

//第一步:定义注解

@interface selfAnnotation{

//第二步:定义参数

int type() default 1;

}

三、读取方法参数的注解

我们都知道,一个函数的参数可以是多个,而一个参数的注解也可以是多个,所以,我们获取到的 参数的注解是一个二维数组,即第几个参数的第几个注解

package test;

import java.lang.annotation.*;

import java.lang.reflect.Method;

import java.util.Arrays;

public class changeData {

public static void main(String[] args) {

Class pClass = persson.class;

try {

Method method_getNum = pClass.getMethod(“getNum”, int.class);

Annotation[][] parameterAnnotations = method_getNum.getParameterAnnotations();

System.out.println(Arrays.toString(parameterAnnotations[0]));

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

}

}

class persson{

private int num;

public int getNum(@two @one int a){

return this.num;

}

}

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.PARAMETER)

@interface one{

int type() default 1;

}

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.PARAMETER)

@interface two{

int value() default 2;

}

四、使用注解

注解如何使用,完全由程序自己决定。例如,JUnit是一个测试框架,它会自动运行所有标记为@Test的方法。

我们来看一个@Range注解,我们希望用它来定义一个String字段的规则:字段长度满足@Range的参数定义:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

public @interface Range {

int min() default 0;

int max() default 255;

}

总结

其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。

这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来

目录:

部分内容截图:


《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来

目录:

[外链图片转存中…(img-MBRGo3OR-1714457980651)]

部分内容截图:

[外链图片转存中…(img-zAIw17Ke-1714457980652)]

[外链图片转存中…(img-37udZ8aS-1714457980652)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值