提供给jvm 识别的并保留标注内容,可以标注类、方法、变量、参数等
**@interface MyAnnotation{}**自定义注解
import java.lang.annotation.*;
//测试元注解
@MyAnnotation
public class Demo1 {
}
//定义一个原注解
//METHOD 注释可以在类上生效
@Target(value = {ElementType.METHOD,ElementType.TYPE})//定义注解在那些地方生效 METHOD方法上有效
//@Retention 表示我们的注解在什么地方还有效
// runtime > class > sources
@Retention(value = RetentionPolicy.RUNTIME)
//表示是否将我们的注解生成在Javadoc 中
@documented
//@Inherited 表示子类可以继承父类的注解
@Inherited
@interface MyAnnotation{
}
package Annotation;
import java.lang.annotation.*;
//自定义注解
@MyAnnotation
public class Demo2 {
@MyAnnotation1("li")
public void test(){
}
}
//元注解,制定注解的属性,作用范围,是否写入注释文档,是否可被子类继承
@Inherited
@documented
//下面两个元注解必须有
@Target(value = ElementType.TYPE)//可在类上使用
// @Target(value = ElementType.METHOD)//可在方法上使用
@Retention(RetentionPolicy.CLASS) //在类中保存该注解信息
@interface MYAnnotation {
//注解的参数:参数类型 + 参数名();
String name(); //无默认值,使用注解时,必须显示定义
String value();//参数名为value时无默认值,调用时可不传值
int id() default 0; //有默认值 使用注解时,可不用传值
}
@interface MyAnnotation1{
String value();//只有一个参数时,参数名尽量为value
}
反射
反射,个人理解为通过一定的暴力手段获得一些不能正常获取的东西,反射是Java 可以称之为伪动态语言的重要组成部分,可以获取一个类中的私有方法,属性,注解,等。
package Annotation;
//测试什么叫反射
public class Demo3 {
public static void main(String[] args) throws ClassNotFoundException {
//通过反射获取类的Class 对象
Class c1 = Class.forName("Annotation.user");
System.out.println(c1);
Class c2 = Class.forName("Annotation.user");
Class c3 = Class.forName("Annotation.user");
Class c4 = Class.forName("Annotation.user");
System.out.println(c2.hashCode());
System.out.println(c3.hashCode());
System.out.println(c4.hashCode());
//一个类在内存中只有一个Class 对象
//一个类被加载后所有的属性和方法被封装在Class 对象中
}
}
//实体类的定义 pojo,entity
class user{
//属性
private String name;
private int id;
private int age;
//无参构造
public user() {
}
//有参构造
public user(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
//get 方法
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getAge() {
return age;
}
//set 方法
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setAge(int age) {
this.age = age;
}
//重写一个tostring 方法,方便调试
}
package Annotation;
//测试Class 类的Class 对象的创建方式有哪些
public class Demo4 {
public static void main(String[] args) throws ClassNotFoundException {
Person s1 = new Student();
System.out.println("这个人是:"+s1.getName());
//方式1,通过对象获取类的Class 对象
Class c1 = s1.getClass();
//方式2,通过类名.class 获取 Class 对象
Class c2 = Person.class;
//方式3,通过完整的包类名:forName()方法获取Class 对象
Class c3 = Class.forName("Annotation.Person");//可能找不到类,故抛出异常
//获得 包装类 的Class 对象 (包装类.TYPE)
Class c4 = Integer.TYPE;
//获得父类型的Class 对象
Class c5 = c1.getSuperclass();
//验证三个Class对象是否相同
System.out.println(c1.hashCode());
System.out.println(c2.hashCode());
System.out.println(c3.hashCode());
System.out.println(c4.hashCode());
System.out.println(c5.hashCode());
}
}
//实体类
class Person{
private String name;
private int age;
private int id;
public Person(){
}
public Person(String name,int age,int id){
this.age = age;
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
", id=" + id +
'}';
}
}
//继承Person 类的子类
class Student extends Person{
public Student(){
this.getName();
}
}
package Annotation;
import java.lang.annotation.ElementType;
public class Demo5 {
public static void main(String[] args) {
//所有类型的Class 对象
Class c1 = Object.class; //类
Class c2 = Runnable.class;//接口
Class c3 = String[].class;//一维数组
Class c4 = int[][].class;//二维数组
Class c5 = Override.class;//注解
Class c6 = ElementType.class;//枚举
Class c7 = Integer.class;//基本数据类型
Class c8 = void.class;//void
Class c9 = Class.class;//Class
//输出
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
System.out.println(c6);
System.out.println(c7);
System.out.println(c8);
System.out.println(c9);
//Class 对象与类型有关,与类型的大小无关
//a.getClass().hashCode()=460141958
//b.getClass().hashCode()=460141958
int[] a = new int[10];
int[] b = new int[100];
System.out.println(a.getClass().hashCode());
System.out.println(b.getClass().hashCode());
}
}
欢迎各位大佬指正


