当我们想要使用别人的东西或者查看某些资源的时候,可以使用反射技术 再比如,开发的时候,有时并不能直接看到源代码,也可以通过反射获取
1.2 反射的前提:获取字节码对象字节码对象获取的3种方式:
类名.class
Class.forName(“目标类的全路径”)
目标类对象.getClass() 注意: 字节码对象是获取目标对象所有信息的入口
获取包对象:clazz.getPackage(): package cn.tedu.reflection
先获取包对象,再获取包对象的名字:clazz.getPackage().getName(): cn.tedu.reflection
获取类名: clazz.getName()–打印的是全路径名,类名,包名 :cn.tedu.reflection.Student clazz.getSimpleName()–打印的只有目标类的类名 :Student
clazz :class cn.tedu.reflection.Student
package cn.tedu.reflection;
import org.junit.Test;
import java.util.Arrays;
public class TestReflection {
@Test
public void getClazz() throws ClassNotFoundException {
//1.练习获取字节码对象的3种方式
Class> clazz1 = Class.forName("cn.tedu.reflection.Student");
Class clazz2 = Student.class;
Class> aClass = new Student().getClass();
//打印的是Student类对应的字节码对象
System.out.println(clazz1);//class cn.tedu.reflection.Student
//获取当前字节码对象的名字
System.out.println(clazz1.getName());//cn.tedu.reflection.Student
//通过字节码对象,获取Student类的类名
System.out.println(clazz1.getSimpleName());//Student
//通过字节码对象,获取Student类对应的包对象
System.out.println(clazz1.getPackage());//package cn.tedu.reflection
//通过字节码对象,先获取Student类对应的包对象,再获取这个包对象的名字
System.out.println(clazz1.getPackage().getName());//cn.tedu.reflection
}
@Test
public void getStu() {
//1.创建学生类对象
Student s1 = new Student("张三", 3);
Student s2 = new Student("李四", 4);
Student s3 = new Student("王五", 5);
//2.准备数组将刚刚创建好的学生对象存入
Student[] s = {s1, s2, s3};
//3.直接打印
System.out.println(Arrays.toString(s));//[Student{name='张三', age=3}, Student{name='李四', age=4}, Student{name='王五', age=5}]
//4.遍历学生数组,拿到每一个学生对象
for (Student student :
s) {
System.out.println(student);
student.eat(3);
student.play();
}
}
}
package cn.tedu.reflection;
public class Student {
//成员变量
String name;
int age;
//2全参构造和无参构造
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
public void play(){
System.out.println("玩代码");
}
public void eat(int n){
System.out.println("中午吃"+n+"碗大米饭");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
2 注解的分类
注解一共分为3大类,我们先来认识一下:
2.1 JDK注解
JDK自带注解
元注解
自定义注解
JDK注解的注解,就5个:
@Override :用来标识重写方法
2.2 元注解用来描述注解的注解,就5个:
@Target 注解用在哪里:类上、方法上、属性上等等
@Retention 注解的生命周期:源文件中、字节码文件中、运行中
2.2.1 @Target ElementType…描述注解存在的位置:
ElementType.TYPE 应用于类的元素
ElementType.METHOD 应用于方法级
ElementType.FIELD 应用于字段或属性(成员变量)
ElementType.ANNOTATION_TYPE 应用于注解类型
ElementType.ConSTRUCTOR 应用于构造函数
ElementType.LOCAL_VARIABLE 应用于局部变量
ElementType.PACKAGE 应用于包声明
ElementType.PARAMETER 应用于方法的参数
package cn.tedu.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class TestAnnotation {
}
//3.通过@Rentention注解标记自定义注解Rice的生命周期
@Retention(RetentionPolicy.RUNTIME)//到运行时都有效
//2.通过@target注解标记自定义注解Rice可以使用的位置
@Target({ElementType.TYPE,ElementType.METHOD})
//1.定义自定义注解
@interface Rice {
//5.我们可以给注解进行功能增强--添加注解的属性
// int age();//给自定义注解添加了一个普通属性age,类型是int
int age() default 0;//给自定义注解的普通属性赋予默认值0
// String value();//定义一个特殊属性value,类型是String
String value() default "秦晓燕";//定义特殊属性,并给特殊属性赋予默认值
}
//4.定义一个类用来测试自定义注解
//@Rice
class TestAnno{
// @Rice 报错了
String name;
// @Rice(age = 10)
// @Rice("秦晓燕")
// @Rice
@Rice(age=10,value = "秦晓燕")
public void eat(){
System.out.println("干饭不积极,思想有问题~");
}
}



