java注解

简介: 注解是什么? 注解是一种语言形式而已,类似于接口、枚举这些语言形式。   类,枚举,接口,注解都是一种语言形式,他们是平行的,既然有形式,那么就应当有规范。 “类”的形式    class  类名 “接口”的形式    interface 接口名 “枚举”的形式  enum 枚举名 “注解”的形式  @interface 注解名   类有类的规则 由于类是使用最多的一种形式,所以里面的规则是比较多的,规则多了变化就多,各种结构,模式,访问控制就可能产生。

注解是什么?

注解是一种语言形式而已,类似于接口、枚举这些语言形式。

image

 

类,枚举,接口,注解都是一种语言形式,他们是平行的,既然有形式,那么就应当有规范。

“类”的形式    class  类名

“接口”的形式    interface 接口名

“枚举”的形式  enum 枚举名

“注解”的形式  @interface 注解名

 

类有类的规则

由于类是使用最多的一种形式,所以里面的规则是比较多的,规则多了变化就多,各种结构,模式,访问控制就可能产生。除了“类”之外的其他三个哥们儿都有些嗜好,进行了一些看起来怪怪的规定,以满足他们干活时特殊的需求。

 

其他三个哥们儿:

接口有接口的规则,如所有的方法都是公有的、抽象的。

枚举有枚举的规则,如枚举元素必须写在枚举大括号的第一行;枚举实现抽象方法的方式。等等。

注解有注解的规则,如 @Target告诉编译器这个注解可以写在哪些成员身上(类,方法,成员变量,局部变量,构造器,包,参数等等详见java语言规范)  ,  @Retention来指定注解保持到什么时期(源代码时期,Class时期,在jvm运行期),为了简便起见使用注解时给value赋值时可以不用写”value=”。

 
自定义个注解  ITCastAnnotation.java
package test.annotation;

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

import test.TestEnum;

/**
 * 注解示例
 * @author liujl
 *
 */
@Target({ElementType.METHOD,ElementType.TYPE})//可以写在方法上,也可以写在类上
@Retention(RetentionPolicy.CLASS)  //注解性质,保留到运行期。.java-->javac-->.class--java-->类装载器装载-->进入"运行期"
public @interface ITCastAnnotation {
	String color() default "blue";//注解属性类型--字符串类型
	String value();//一个特殊的注解属性类型,可以默认不写
	int[]  arrayAttr() default {1,2,3};//注解属性类型--数组类型
	TestEnum.Triffic  lamp() default TestEnum.Triffic.RED;
	MetaAnnotation annotationAttr() default @MetaAnnotation("lhm");//注解属性类型--注解类型 
	Class clazz() default String.class;//注解属性类型--Class类型
}

 

 

 

使用自己定义的注解 AnnotationTest.java
package test.annotation;

@ITCastAnnotation(annotationAttr=@MetaAnnotation(value="fxx") ,value="111",arrayAttr={2,3,4})
public class AnnotationTest {
	public static void main(String[] args)throws Exception {
		System.runFinalizersOnExit(true);
		if(AnnotationTest.class.isAnnotationPresent(ITCastAnnotation.class))
		{
			ITCastAnnotation ia=AnnotationTest.class.getAnnotation(ITCastAnnotation.class);
			System.out.println(ia.value());
			System.out.println(ia.color());
			
			int[] arrayAttr=ia.arrayAttr();
			for(Integer i:arrayAttr){
				System.out.print(i+" ");
			}
			System.out.println();
			
			System.out.println(ia.lamp().nextDeng());
			
			System.out.println(ia.annotationAttr().value());
			
			System.out.println(ia.clazz());
		}
	}
}

 

 

TestEnum.Triffic 是一个枚举类,代码如下
package test;

public class TestEnum {
	public static void main(String[] args) {
		Triffic tr=Triffic.GREEN;
		System.out.println(tr+"  下一个灯是: "+tr.nextDeng());
	}
	
	public enum Triffic{
		RED (400){
			@Override
			public Triffic nextDeng() {
				return GREEN;
			}
		},
		GREEN (200){
			@Override
			public Triffic nextDeng() {
				return YELLOW;
			}
		},
		YELLOW(10) {
			@Override
			public Triffic nextDeng() {
				return RED;
			}
		};
		
		public abstract  Triffic nextDeng();
		int time;
		private Triffic(int time){
			this.time=time;
		}
		public int getTime() {
			return time;
		}
		
		public String toString(){
			
			return this.name()+":"+this.time;
		}
	}
	
	
}

 

MetaAnnotation.java 是一个自定义的元注解 ,在ITCastAnnotation 中作为注解的一个属性值类型而存在
package test.annotation;
/**
 * 定义一个元注解
 * @author liujl
 *
 */
public @interface MetaAnnotation {
	String value();
}

 

运行结果:

111
blue
2 3 4
GREEN:200
fxx
class java.lang.String

 

 

 

学习资料

1.张孝祥老师的视频

2.《Effective  Java@2008 第二版》

I N release 1.5, two families of reference types were added to the language: a new
kind of class called an enum type, and a new kind of interface called an annotation
type. This chapter discusses best practices for using these new type families.

 

Item 30: Use enums instead of int constants
An enumerated type is a type whose legal values consist of a fixed set of con-
stants, such as the seasons of the year, the planets in the solar system, or the suits
in a deck of playing cards. Before enum types were added to the language, a com-
mon pattern for representing enumerated types was to declare a group of named
int constants, one for each member of the type:
// The int enum pattern - severely deficient!
public static final int APPLE_FUJI  = 0;
public static final int APPLE_PIPPIN  = 1;
public static final int APPLE_GRANNY_SMITH = 2;
public static final int ORANGE_NAVEL  = 0;
public static final int ORANGE_TEMPLE = 1;
public static final int ORANGE_BLOOD  = 2;
This technique, known as the int enum pattern, has many shortcomings. It
provides nothing in the way of type safety and little in the way of convenience.
The compiler won’t complain if you pass an apple to a method that expects an
orange, compare apples to oranges with the == operator, or worse:
// Tasty citrus flavored applesauce!
int i = (APPLE_FUJI - ORANGE_TEMPLE) / APPLE_PIPPIN;

开始做,坚持做,重复做
相关文章
|
11月前
|
XML Java 编译器
Java注解的底层源码剖析与技术认识
Java注解(Annotation)是Java 5引入的一种新特性,它提供了一种在代码中添加元数据(Metadata)的方式。注解本身并不是代码的一部分,它们不会直接影响代码的执行,但可以在编译、类加载和运行时被读取和处理。注解为开发者提供了一种以非侵入性的方式为代码提供额外信息的手段,这些信息可以用于生成文档、编译时检查、运行时处理等。
224 7
|
8月前
|
Java 编译器 开发者
注解的艺术:Java编程的高级定制
注解是Java编程中的高级特性,通过内置注解、自定义注解及注解处理器,可以实现代码的高度定制和扩展。通过理解和掌握注解的使用方法,开发者可以提高代码的可读性、可维护性和开发效率。在实际应用中,注解广泛用于框架开发、代码生成和配置管理等方面,展示了其强大的功能和灵活性。
188 25
|
XML Java 编译器
Java学习十六—掌握注解:让编程更简单
Java 注解(Annotation)是一种特殊的语法结构,可以在代码中嵌入元数据。它们不直接影响代码的运行,但可以通过工具和框架提供额外的信息,帮助在编译、部署或运行时进行处理。
273 43
Java学习十六—掌握注解:让编程更简单
|
11月前
|
Java 编译器 数据库
Java 中的注解(Annotations):代码中的 “元数据” 魔法
Java注解是代码中的“元数据”标签,不直接参与业务逻辑,但在编译或运行时提供重要信息。本文介绍了注解的基础语法、内置注解的应用场景,以及如何自定义注解和结合AOP技术实现方法执行日志记录,展示了注解在提升代码质量、简化开发流程和增强程序功能方面的强大作用。
384 5
|
Arthas Java 测试技术
Java字节码文件、组成,jclasslib插件、阿里arthas工具,Java注解
Java字节码文件、组成、详解、分析;常用工具,jclasslib插件、阿里arthas工具;如何定位线上问题;Java注解
Java字节码文件、组成,jclasslib插件、阿里arthas工具,Java注解
|
JSON Java 数据库
java 常用注解大全、注解笔记
关于Java常用注解的大全和笔记,涵盖了实体类、JSON处理、HTTP请求映射等多个方面的注解使用。
347 0
java 常用注解大全、注解笔记
|
Java 编译器 程序员
Java注解,元注解,自定义注解的使用
本文讲解了Java中注解的概念和作用,包括基本注解的用法(@Override, @Deprecated, @SuppressWarnings, @SafeVarargs, @FunctionalInterface),Java提供的元注解(@Retention, @Target, @Documented, @Inherited),以及如何自定义注解并通过反射获取注解信息。
Java注解,元注解,自定义注解的使用
|
Java 编译器
Java进阶之标准注解
Java进阶之标准注解
118 0
|
Java 编译器 测试技术
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit