JavaSE-注解

package com.btp.t4;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

/*
 * 元数据:修饰实体的修饰词
 * 
 * 注解
 * 1.JDK提供的常用的注解
 *   @Override:限定重写父类方法,该注释只能用于方法
 *   @Deprecated:用于表示某个程序元素(类,方法等)已过时。但是还是可以调用这些程序元素。
 *                只是告诉你这些程序元素在将来的某一天会弃用
 *   @SuppressWarning:抑制编译器警告
 * 2.如何自定义一个注解: @interface
 * 3.元注解:用于修饰其它Annotation定义
 *   @Retention
 *   @Target
 *   @Documented
 *   @Inherited
 *    *   
 */
public class TestAnnotation {
	public static void main(String[] args){
		Human hum=new Student();
		hum.walk();
		hum.eat();//还是会执行
		
		@SuppressWarnings({ "rawtypes", "unused" })
		//一个警告是“没有使用泛型“,另一个是”还没有用“。@SuppressWarnings是抑制这些警告
		List list=new ArrayList();
	}
}
@MyAnnotation(value="btp-java")
class Student extends Human{
	@Override
	public void walk(){
		System.out.println("学生走路");
	}
	@Override
	public void eat(){
		System.out.println("学生吃饭");
	}
}

class Human{
	String name;
	int age;
	public void walk(){
		System.out.println("走路");
	}
	@Deprecated
	public void eat(){
		System.out.println("吃饭");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Human(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Human() {
		super();
		// TODO 自动生成的构造函数存根
	}
	@Override
	public String toString() {
		return "Human [name=" + name + ", age=" + age + "]";
	}
	
}

//自定义注解
@Retention(RetentionPolicy.CLASS)
@interface MyAnnotation{
	String value() default "hello";
}

  

原文地址:https://www.cnblogs.com/a842297171/p/5170345.html