IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> java注解快速入门 -> 正文阅读

[游戏开发]java注解快速入门

注解相关信息

  • @Documented – 存在于JavaDoc
  • @Retention – 注解使用场景
  • @Target – 目标地址
  • @Inherited – 是否允许子类继承该注解

Retention定义注解

指示带注释类型的注释将保留多长时间。 如果注释类型声明中不存在 Retention 注释,则保留策略默认为RetentionPolicy.CLASS 。


package java.lang.annotation;

/**
 * Annotation retention policy.  The constants of this enumerated type
 * describe the various policies for retaining annotations.  They are used
 * in conjunction with the {@link Retention} meta-annotation type to specify
 * how long annotations are to be retained.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

● RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
● RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
● RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

Target描述注解使用场景

  • ElementType.CONSTRUCTOR: 用于描述构造器

  • ElementType.FIELD: 成员变量、对象、属性(包括enum实例)

  • ElementType.LOCAL_VARIABLE: 用于描述局部变量

  • ElementType.METHOD: 用于描述方法

  • ElementType.PACKAGE: 用于描述包

  • ElementType.PARAMETER: 用于描述参数

  • ElementType.TYPE: 用于描述类、接口(包括注解类型) 或enum声明

自定义注解举例

自定义注解

package com.itdfq.annotation;


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

/**
 * @Author: GodChin
 * @Date: 2021/9/15 15:36
 * @Description:
 */

@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface GetName {
    String name();
    String value();
}

注解类

package com.itdfq;

import com.itdfq.annotation.GetName;

/**
 * @Author: QianMo
 * @Date: 2021/9/15 9:17
 * @Description: 自定义注解
 */
@GetName(name = "Demo1类",value = "类属性注解")
public class Demo1 {
    @GetName(name = "name字段",value = "属性注解")
    private String name;
    @GetName(name = "test()方法注解",value = "方法注解")
    public void test(){
        System.out.println("123");
    }
    public void hello(){
        System.out.println("hello world");
    }
}

获取注解信息

package com.itdfq.annotation;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author: QianMo
 * @Date: 2021/9/16 9:39
 * @Description:  通过反射获取注解信息
 */
public class ClientTest {
    public static void main(String[] args) throws Exception {
        Class aClass = Class.forName("com.itdfq.Demo1");
        //获取注解类信息
        System.out.println("------------获取注解类信息-----------");
        GetName annotation = (GetName) aClass.getAnnotation(GetName.class);
        System.out.println(annotation.name());
        System.out.println("------------获取方法注解-----------");
        //获取方法注解   使用isAnnotationPresent判断方法上是否使用了注解
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (int i = 0 ; i<declaredMethods.length;i++){
            if (declaredMethods[i].isAnnotationPresent(GetName.class)){
                GetName getName = declaredMethods[i].getAnnotation(GetName.class);
                System.out.println(JSON.toJSONString(getName));
            }
        }
        System.out.println("------------获取指定方法注解信息-----------");
        //获取指定方法注解信息
        Method test = aClass.getDeclaredMethod("test");
        GetName annotation1 = test.getAnnotation(GetName.class);
        System.out.println(JSON.toJSONString(annotation1));
        //获取指定属性字段信息
        System.out.println("------------获取指定属性字段信息-----------");
        Field name = aClass.getDeclaredField("name");
        GetName annotation2 = name.getAnnotation(GetName.class);
        System.out.println(JSON.toJSONString(annotation2));
        System.out.println("------------获取所有字段信息-----------");
        Field[] declaredFields = aClass.getDeclaredFields();

    }
}

结果

结果

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-18 10:33:04  更:2021-09-18 10:35:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 19:50:23-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码