关于注解的简析

关于注解的简析

第一次写博客,瞎写

首先,注解是什么?注解和注释有区别吗?
注解就是给自己还有给处理器看的语句。
而注释呢,只能让自己看到,而编译器会忽略掉这些注释

内置注解有三种,

1、@Override也就重写;主要来重写父类里面有的方法;
比如使用很多的重写toString方法:

@Override
public void toString(){
	return "";
}

2、@Deprecate,它表示不鼓励使用这些方法的,但是被它提示的方法还是能用。

@Deprecated
	public static void test001() {
		System.out.println("it run");
	}

3、@SuppressWarnings,下压警告,也就是把提示的警告去除,
在这里插入图片描述
这些就看不到了。

那么,怎么写注解呢?

注解还有三个元注解

@Target(就是表示编译器需要检查的类型)
有PACKAGE(包)、TYPE(类、枚举、接口)、CONSTRUCTOR(构造器)、FIELD(描述域)、METHOD(方法)、LOCAL_VARIABLE(局部变量)、PARAMETER(参数)

在文件中用@Target(value = ElementType.TYPE)表示

@Retention(表示运行的时候)
所以,打开ecplise的注释;编译Annotation注释文件:

@Target(value = {ElementType.METHOD,ELementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation{
	//String Value();正常情况下只有一个值用value();
	String studentName() defalut "";
	int age() defalut 0;
	int id() default -1;//-1表示没有
}

然后在写一个测试的文件demo1

class demo1{
	@Annotation(studentName="凡",age = 21,id=1001)
	public void test01(){
		
	}
}

这样就了解的注释了,那么现在开始讨论如何让编译器读到你的注释
首先我们写一个student类。一个表格中(tb_student)中有id、sName、age这三种属性,并让编译器检查是看看这些属性是否有正确的格式
1、写一个student类:

public class student{
	private int id;
	private String studentName;
	private int age;


	public int getId() {
			return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudengName(String studentName) {
		this.studentName = studentName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

写一个检查表格属性的注解:

@Target(value = {ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface sField{
	String colunmName();
	String type();
	int length();
}

然后开始给student附上注解:

public class student{
	@Annotiation(colunmName = "id",type = int,length = 10)
	private int id;
	@Annotiation(colunmName = "studentName",type = String,length = 10)
	private String studentName;
	@Annotiation(colunmName = "id",type = int,length = 10)
	private int age;


	public int getId() {
			return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudengName(String studentName) {
		this.studentName = studentName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

按上面方法检查表格是否是tb_student:

@Target(value = {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table{
	String value();
}
@Table("tb_student")
public class Student {
	
	@sField(colunmName="id",type="int",length=10)
	private int id ;
	@sField(colunmName="sName",type="varchar",length=10)
	private String studentName;
	@sField(colunmName="age",type="int",length=3)
	private int age;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudengName(String studentName) {
		this.studentName = studentName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
	
}

OK!
接下来写一个demo3开始测试:

public class Demo03 {

	public static void main(String[] args) {
		
		try {
			Class clazz = Class.forName("Annotation.Student");//包.类
			
			//获得类的所有注解
			Annotation[] annotation = clazz.getAnnotations();
			for(Annotation a:annotation){
				System.out.println(a);
			}
			
			//获得类指定的注解
			Table t = (Table)clazz.getAnnotation(Table.class);
			System.out.println(t.value());
			
			//获得类的属性的注解
			Field f = clazz.getDeclaredField("studentName");
			sField sfield = f.getAnnotation(sField.class);
			
			System.out.println(sfield.colunmName()+"--"+sfield.type()+"--"+sfield.length());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}

按F11运行得结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值