java学习之注解

这篇文章我们来谈谈java的注解,java的注解分为三种一种是source,也就是在源文件的时候有效,还有一种是class,是在class中有效,当然我们写的都是最后一种也就是runtime,也就是运行时有效的,这里我只是写了一个简单的例子来作为注解的小的自定义注解demo,这里并没有仔细去研究这个,因为毕竟我们在很多时候是不需要写注解的,但是知道对我们并没有什么坏处。

对了这里顺便说说java里面的三个注解:

override,对方法的重写的检查

deprecated,对不再使用的方法进行注解

suppressWarnings 基本上就是消除一些警告的

大概就这样,看看一个简单的自定义annotation的代码吧

代码:

package www.jk.annotation;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

//define the action scope of the myFactor
@Retention(value = RetentionPolicy.RUNTIME)
// define the Annotation
@interface myFactor {
	public String name() default "ni hao ";

	public String val();
}

public class Test {
	public static void main(String[] args) {
		// get the Class object of the Stu
		Class<?> cla = Stu.class;
		// get the Annotations of the Stu
		Annotation an[] = cla.getAnnotations();
		for (int i = 0; i < an.length; i++) {
			// print each annotation
			System.out.println(an[i]);
		}

	}

}

@myFactor(val = "what")
class Stu {
	String name = "wang";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值