一、注解
一、概念与作用
1.类似注释:对程序进行解释
2.不同注释:可以被其他程序[编译器/Spring 容器]使用处理,用作不同的用途
3.作用:
1.很多框架提供注解给开发者增强功能
2.简洁的方式,给程序提供强大的功能和灵活性,提高开发效率
4.定义方式:使用@interface
5.使用形式:@注解名(属性名=属性值)
二、JAVA注解
1.@Override:子类重写父类方法
2.@Deprecated:可以使用,但不推荐继续使用,即将被放弃维护且删除接口
3.@SuppressWarnings:抑制警告输出打印提示
三、元注解
1.修饰注解的注解
2.@Target:
1.使用目标对象,
2.可用枚举属性值为TYPE、FIELD、METHOD、PARAMETER、CONSTRUCTOR 等
3.@Retention:
1.需保留到的最后期限
2.可用枚举属性值为SOURCE、CLASS、RUNTIME
4.@Documentation:是否需要输出到Javadoc
5.@Inherited:父类的注解[带有被@Inherited 注解]可以被子类继承
四、自定义注解
1.定义:使用@interface
2.定义属性:类名+名字()
3.赋默认值:default + 默认值
4.修饰:使用需要的元注解
二、 反射
一、概念、作用
1.反射是能反应对象所属类的完整结构信息[含private ]
2.程序运行时,获取类的完整结构信息,如方法、属性、注解等,还能执行方法和修改属性
public class User {
public int id;
private String name;
private int age;
private int sex;
public String createTime;
public String getName(){ return name;}
public int getAge(){return age;}
public void setAge(int age){this.age=age;}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
二. 反射类和反射对象
1.反射对应的类是Class 类,每个类型对应唯一一个Class 对象,即反射对象
2.反射对象创建方式:
1.Class.forName(“包名.类名”)
2.类名.class
3.对象.getClass()
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException {
Class cls=Class.forName("Test01");
Class<Test01> test01Class=Test01.class;
Test01 test01=new Test01();
Class aClass=test01.getClass();
}
}
三. 获取方法、属性
1.新建User 类
2.获取User 类所有方法和属性
1.反射对象.getDeclaredMethods()
2.反射对象.getDeclaredFields()
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException {
Class aClass=Class.forName("User");
Method[] declaredMethods=aClass.getDeclaredMethods();
for(Method declaredMethod:declaredMethods){
System.out.println(declaredMethod);
}
System.out.println("==============================");
Field[] declaredClasses=aClass.getDeclaredFields();
for(Field declaredClass:declaredClasses){
System.out.println(declaredClass);
}
}
}
四. 执行方法和设置属性
1.构造真实类对象:反射对象.newInstance()
2.执行方法:方法对象.invoke(对象,参数值)
3.设置属性:属性对象.set(对象,属性值)
4.属性可被修改:属性对象.setAccessible(true)
public class Test03 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class aClass=Class.forName("User");
User user=(User)aClass.newInstance();
Method setName=aClass.getDeclaredMethod("setName", String.class);
setName.invoke(user,"zhangsan");
System.out.println(user.getName());
System.out.println("=====================");
Field age=aClass.getDeclaredField("age");
age.setAccessible(true);
age.set(user,18);
System.out.println(user.getAge());
}
}
五. 获取注解对象和属性
1.使用前面注解@MyAnnotation 作用到User 类上
2.获取注解对象:反射对象.getDeclaredAnnotation(注解的反射对象)
3.获取注解属性:注解对象.属性
public class Test04 {
public static void main(String[] args) throws ClassNotFoundException {
Class aClass=Class.forName("User");
Annotation declaredAnnotation=aClass.getDeclaredAnnotation(MyAnnotation.class);
if(declaredAnnotation instanceof MyAnnotation){
MyAnnotation myAnnotation=(MyAnnotation)declaredAnnotation;
System.out.println(myAnnotation.name());
}
if(declaredAnnotation instanceof RestController){
System.out.println("declaredAnnotation is a RestController annotation");
}else{
System.out.println("declaredAnnotation is not a RestController annotation");
}
}
}