• java注解


    注解是什么?

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

    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;

  • 相关阅读:
    OpenGL红宝书例子2.2 uniform变量的使用
    感冒了。。。
    OpenGL红宝书第一个例子:绘制两个三角形
    从今日起,我会把OpenGL红宝书上的例子用完整的代码形式写在我的博客中,
    win8.1下安装ubuntu 14.0 4LTS
    windows下编译Android版本的boost库文件
    cocos2d-x中使用tinyxml遇到的问题及解决
    为申请texturepacker用
    安装MSYS2过程遇到的问题及解决记录
    Lua 基础 -- 学习笔记
  • 原文地址:https://www.cnblogs.com/qq-757617012/p/5772830.html
Copyright © 2020-2023  润新知