博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java注解Annotation简介
阅读量:2492 次
发布时间:2019-05-11

本文共 2188 字,大约阅读时间需要 7 分钟。

注解定义和使用

注解定义为:

public @interface AnnotationName{}

使用注解

@ AnnotationNamepublic void func(){}

Java提供了4个元注解

@Retention:保留的阶段。

@Target:注解修饰的目标,可以说类,方法,成员变量,包。
@Documented:是否被javadoc提取成文的。
@Inherited:注解是否能继承。

自定义注解

注解的成员变量定义:

public @interface AnnotationName{    String name() default "abc";    int age() default 33;    boolean flag();}

其中default修饰的表示默认值。

  • 注意:Annotation修饰类,方法,成员变量后不会自己生效,不会对程序有任何影响。只有为这些注解提供处理的工具类才会有用。

实例

定义注解类

import java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface Testable {
boolean isSkiped() default false; String name();}

被注解修饰的类

public class Foo {    public static void m1() {        System.out.println("I am m1");    }    @Testable(isSkiped = false, name = "methond2")    public static void m2() {        System.out.println("I am m2");    }    @Testable(name = "methond3")    public static void m3() {        System.out.println("I am m3");    }    @Testable(isSkiped = true, name = "methond4")    public static void m4() {        System.out.println("I am m4");    }}

处理注解的工具类

import java.lang.reflect.*;public class TestAnnotation {    public static void main(String[] args) throws SecurityException,            ClassNotFoundException, IllegalAccessException,            IllegalArgumentException, InvocationTargetException {        for (Method m : Class.forName("Foo").getMethods()) {            if (m.isAnnotationPresent(Testable.class)) {                System.out.print(m.getName()                        + " is Annotation element, so invoke result is: ");                m.invoke(null);            } else {                System.out.println(m.getName() + " is not Annotation element");            }        }    }}

输出:

m1 is not Annotation elementm2 is Annotation element, so invoke result is: I am m2m3 is Annotation element, so invoke result is: I am m3m4 is Annotation element, so invoke result is: I am m4wait is not Annotation elementwait is not Annotation elementwait is not Annotation elementequals is not Annotation elementtoString is not Annotation elementhashCode is not Annotation elementgetClass is not Annotation elementnotify is not Annotation elementnotifyAll is not Annotation element

转载地址:http://pvhrb.baihongyu.com/

你可能感兴趣的文章
通知机制 (Notifications)
查看>>
10 Things You Need To Know About Cocoa Auto Layout
查看>>
一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
查看>>
iOS 如何放大按钮点击热区
查看>>
ios设备唯一标识获取策略
查看>>
获取推送通知的DeviceToken
查看>>
Could not find a storyboard named 'Main' in bundle NSBundle
查看>>
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Swift code into Object-C 出现 ***-swift have not found this file 的问题
查看>>
为什么你的App介绍写得像一坨翔?
查看>>
RTImageAssets插件--@3x可自动生成@2x图片
查看>>
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>