一、注解
1.概念、作用
1.类似注释:对程序进行解释
2.不同注释:可以被其他程序[编译器/Spring 容器]使用处理,用作不同的用途
3.作用:
1.很多框架提供注解给开发者增强功能
2.简洁的方式,给程序提供强大的功能和灵活性,提高开发效率
4.定义方式:使用@interface
5.使用形式:@注解名(属性名=属性值)
2.Java 注解
1.@Override:子类重写父类方法
2.@Deprecated:可以使用,但不推荐继续使用,即将被放弃维护且删除接口
3.@SuppressWarnings:抑制警告输出打印提示
//内置注解
public class Test01 extends Object{
//@Override 重写注解
@Override
public String toString(){
return super.toString();
}
//@Deprecated 不推荐程序员使用,但可以使用,或者更好的方法
@Deprecated
public static void test(){
System.out.println("Deprecated");
}
//@SuppressWarning 镇压警告
@SuppressWarnings("all")
public void test02(){
List list = new ArrayList();
}
public static void main(String[] args) {
test();
}
}
3.元注解
元注解是用于定义注解的注解,包括@Retention、@Target、@Inherited、@Documented、@Repeatable 等。
元注解也是Java自带的标准注解,只不过用于修饰注解,比较特殊。
4.自定义注解
1.定义:使用@interface
2.定义属性:类名+名字()
3.赋默认值:default + 默认值
4.修饰:使用需要的元注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name() default "";
}
二、反射
1.概念、作用
1.反射是能反应对象所属类的完整结构信息[含private ]
2.程序运行时,获取类的完整结构信息,如方法、属性、注解等,还能执行方法和修改属性
public class Student {
//成员变量
private String name;
private int age;
public int sex;
public String createTime;
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 int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
2.反射类和反射对象
1.反射对应的类是Class 类,每个类型对应唯一一个Class 对象,即反射对象
2.反射对象创建方式:
1.使用类的class属性来获取该类对应的Class对象。 2.调用对象的getClass0方法, 3.使用Class类中的静态方法orName(String className),
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException {
Class<Student> c1 = Student.class;
System.out.println(c1);
System.out.println("=============");
Student s = new Student();
Class<? extends Student> c2 = s.getClass();
System.out.println(c2);
System.out.println("=============");
Class<?> c3 = Class.forName("com.example.demo1.itlianxi07.Student");
System.out.println(c3);
}
}
3.获取方法、属性
1.
新建
Student
类
2.
获取Student 类所有方法和属性
1.
反射对象
.
getDeclaredMethods
()
2.
反射对象
.
getDeclaredFields
()
/**
* 反射对象.getDeclaredMethods()
* 反射对象.getDeclaredFields()
*/
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
Class aClass = Class.forName("com.example.demo1.itlianxi07.Student");
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);
}
}
}
4.执行方法和设置属性
1.
构造真实类对象:反射对象
.
newInstance
()
2.
执行方法:方法对象
.invoke(
对象,参数值
)
3.
设置属性:属性对象
.set(
对象,属性值
)
4.
属性可被修改:属性对象
.
setAccessible
(true)
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class aClass = Class.forName("com.example.demo1.itlianxi07.Student");
//通过Class 对象构造其真实类Student 的对象
Student student = (Student)aClass.newInstance();
Method setName = aClass.getDeclaredMethod("setName", String.class);
setName.invoke(student,"zhangsan");
System.out.println(student.getName());
System.out.println("====================");
Field age = aClass.getDeclaredField("age");
age.setAccessible(true);
age.set(student,18);
System.out.println(student.getAge());
}
}
5.获取注解对象和属性
1.
使用前面注解
@MyAnnotation
作用到
User
类上
2.
获取注解对象:反射对象
.
getDeclaredAnnotation
(
注解的反射对象
)
3.
获取注解属性:注解对象
.
属性
public class ReflectDemo3 {
public static void main(String[] args) throws ClassNotFoundException {
//反射对象
Class aClass = Class.forName("com.example.demo1.itlianxi07.Student");
//获取注解
Annotation declaredAnnotation = aClass.getDeclaredAnnotation(MyAnnotation.class);
if (declaredAnnotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) declaredAnnotation;
System.out.println(myAnnotation.name());
}
if (declaredAnnotation instanceof Repeatable){
System.out.println("declaredAnnotation is a RestController annotation");
}else {
System.out.println("declaredAnnotation is not a RestController annotation");
}
}
}