javaSE注解的学习

javase是基础,注解在我们java学习道路中是很常见的,在座web开发的时候就会用到。

注解的使用方便了我们开发。



下面就简单演示下注解的定义和对注解中德属性进行赋值并取出来。


创建注解类:(MyAnntation)

package com.ydcun.day2;

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

import com.ydcun.test;


@Retention(RetentionPolicy.RUNTIME)//注解保留到什么时候
@Target({ElementType.METHOD,ElementType.TYPE})//这个注解放的位置,方法和type
public @interface InMyAnnotation {
	String b() default "aa";
	int[] arr();
	String value();//单独的时候赋值可以不指定名称
	test.TrefficLamp lamp() default test.TrefficLamp.RED;//用枚举类型
	Elment elment();//用注解类型
}

创建一个类来使用这个注解

package com.ydcun.day2;

import javax.annotation.security.RolesAllowed;
import javax.ejb.Remote;


import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Annotated;

@InMyAnnotation(arr=1,value = "ddd",elment = @Elment("ydcun"))
public class ZJ {
	@Deprecated
	public void S(){
		
	}
	@SuppressWarnings("deprecation")//过时的继续用
	public static void main(String[] args) {
		System.runFinalizersOnExit(true);//这个是过时的
		if(ZJ.class.isAnnotationPresent(InMyAnnotation.class)){
			InMyAnnotation inMyAnnotation = ZJ.class.getAnnotation(InMyAnnotation.class);//获取到注解,与注解的生命周期有关
			System.out.println(inMyAnnotation);
			System.out.println(inMyAnnotation.arr().length);
			System.out.println(inMyAnnotation.b());
			System.out.println(inMyAnnotation.value());
			System.out.println(inMyAnnotation.lamp().nextLamp().name());
			System.out.println(inMyAnnotation.elment().value());
		}
	}
}

上面最后一个注解型的属性类型定义如下:(Elment)


package com.ydcun.day2;

public @interface Elment {
	String value();
}	

注解这只是定义和给属性复制,后续会有深入应用









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值