2022-8-10第一组孙乃宇
注解和反射
注解:Annotation,Java标注,JDK5引入的一种机制。
Java中类,方法,变量,参数,包都可以被标注
元注解:专门给注解加的注解
我们发现注解中可以有方法,
1、定义方法的格式:String name();
2、可以有默认值,也可以没有,如果没有默认值在使用的时候必须填写对应的值 如果需要有默认值,使用default指定默认值。
如果不学习反射,注解没啥用!!!
在java的整个的注解体系中,有3个非常重要的主干类,
1、Annotation 接口,定义一些常用的方法
2、ElementType 枚举 它用来指定注解的类型。说人话,我的注解要用在哪里???
3、RetentionPolicy 枚举 它用来指定注解的策略。不同类型的策略指定的注解的作用域不同。
(1)SOURCE,注解仅存在于编译器处理期间,编译期处理完之后,这个注解就没用了 (2)CLASS,注解在.class文件中依然有效。
(3)RUNTIME,编译期是不起作用的,只有在运行期才由JVM读取。
Java自带的注解,10个。4个注解java.lang.annotation,6个注解在java.lang
作用在代码上的注解
1、@Override,检查该方法是否是重写方法。如果返现其父类,或者是引用的接口中没有该方法,会报错
2、@Deprecated,标记的方法,过时的方法。
3、@SuppressWarnings编辑器去忽略警告
4、@SafeVarargs,JDK7支持忽略任何使用参数为泛型变量的方法或构造器产生的警告
5、@FunctionalInterface,JDK8开始支持,表示一个接口为函数式接口
6、@Repeatable,JDK8开始支持,标识某个注解可以在同一个声明上使用多次
all:忽略所有警告
boxing:忽略装箱、拆箱警告
rawtypes:使用生成时没有指定数据类型
unchecked:忽略没有进行类型检查操作的警告
unused:忽略没有使用的警告
元注解:
1、@Retention:标识这个注解作用域
2、@Documented:标记这个注解是否包含在用户文档中
3、@Target:这个注解可以修饰哪些信息
4、@Inherited:如果一个类用上了@Inherited注解,那么其子类也会继承这个注解
public class Ch01 {
操作类对象
public class Ch02 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
// 获取类对象
// 1.直接使用类名.class
Class<Dog> clazz = Dog.class;
// 对类对象操作
// 获取类的名字
System.out.println(clazz.getName());
// 获取类的加载器
ClassLoader classLoader = clazz.getClassLoader();
System.out.println(classLoader);
// 获取资源
URL resource = clazz.getResource("");
// 获取父类
System.out.println(clazz.getSuperclass());
// 判断一个类是不是接口,数组
System.out.println(clazz.isArray());
System.out.println(clazz.isInterface());
// 重点,使用class类对象实例化一个对象
对成员变量的操作
public class Ch03 {
public static void main(String[] args) throws NoSuchFieldException {
// 对成员变量的操作
Class<Dog> clazz = Dog.class;
// 只能获取到public的属性
Field type = clazz.getField("type");
System.out.println(type);
Field[] fields = clazz.getFields();
System.out.println(Arrays.toString(fields));
// 可以获取到private属性
Field name = clazz.getDeclaredField("name");
System.out.println(name);
System.out.println(name.getType());
Field[] declaredFields = clazz.getDeclaredFields();
System.out.println(Arrays.toString(declaredFields));
}
}
获取对象的属性
public class Ch04 {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Dog dog = new Dog();
dog.setType("金毛");
Dog dog1 = new Dog();
dog1.setType("泰迪");
Class<Dog> clazz = Dog.class;
Field type = clazz.getDeclaredField("type");
// 想要获取哪一个对象的color
String str = (String) type.get(dog1);
System.out.println(str);
}
}
获取执行方法
public class Ch06 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, InstantiationException {
Class dogClass = Class.forName("com.jsoft.morning.Dog");
Dog dog = (Dog) dogClass.newInstance();
Class<Dog> clazz = Dog.class;
Method show = clazz.getMethod("show", String.class);
// System.out.println(show.getParameterCount());
// System.out.println(show.getName());
Class<?>[] parameterTypes = show.getParameterTypes();
// System.out.println(Arrays.toString(parameterTypes));
// 执行方法,使用反射调用方法
show.invoke(dog,"hello");
Method[] methods = clazz.getMethods();
Method fun = clazz.getDeclaredMethod("fun");
fun.setAccessible(true);
fun.invoke(dog);
Method[] declaredMethods = clazz.getDeclaredMethods();
Method info = clazz.getDeclaredMethod("info");
String o = (String) info.invoke(dog);
System.out.println(o);
}
}
通过反射调用构造器
public class Ch07 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<Dog> clazz = Dog.class;
Constructor<?>[] constructors = clazz.getConstructors();
// System.out.println(Arrays.toString(constructors));
Constructor<Dog> declaredConstructor = clazz.getDeclaredConstructor();
// 通过构造器创建对象
Dog dog = declaredConstructor.newInstance();
// 单例模式
// 1.构造器私有化
Constructor<Dog> declaredConstructor1 = clazz.getDeclaredConstructor(String.class);
declaredConstructor1.setAccessible(true);
Dog dog1 = declaredConstructor1.newInstance("小强");
System.out.println(dog1.getName());
}
}