java注解与反射学习
java注解与反射学习
跟随b站“遇见狂神说”学习,地址https://www.bilibili.com/video/BV1V4411p7EF
新手上路,才学疏浅,望斧正
1 注解
1.1 内置注解
@Override
表示方法声明旨在覆盖超类型中的方法声明。如果使用此注释类型注释方法,则除非至少满足以下条件之一,否则需要编译器生成错误消息:
- 该方法将覆盖或实现在超类型中声明的方法。
- 该方法具有与Object中声明的任何公共方法的覆盖相同的签名 。
@ Deprecated
注释@Deprecated的程序元素是程序员不鼓励使用的程序元素,通常是因为它是危险的,或者因为存在更好的替代方法。编译器在不被弃用的代码中使用或覆盖不推荐使用的程序元素时发出警告。
@SuppressWarnings
表示在注释元素(以及注释元素中包含的所有程序元素)中应该抑制命名的编译器警告。
1.2 元注解
@Target
用于描述主角的使用范围
@Retention
用于表示在什么级别保存该注解信息。(SOURCE<CLASS<RUNTIME)
@Document
说明该主机将被包含在javadoc中
@Inherited
说明子类可以继承父类中的该注解
1.3 自定义注解
package com.demo9;
import java.lang.annotation.*;
/**
* @PackageName: com.demo9
* @ClassName: TestMetaAnnotations
* @author:
* @date:
* @Description:
*/
public class TestMetaAnnotations {
@myAnnotations(name = "001")
public void test(){ }
@myAnnotations2("001")
public void test2(){}
}
@Target(value = {ElementType.METHOD,ElementType.TYPE}) //注解的作用域 方法+类
@Retention(value = RetentionPolicy.RUNTIME) //注解作用级别:运行时
@interface myAnnotations {
//注解参数:参数类似:参数名
String name();
//带默认值的参数
int id() default -1;
String[] school() default {};
}
@Target(value = {ElementType.METHOD,ElementType.TYPE}) //注解的作用域 方法+类
@Retention(value = RetentionPolicy.RUNTIME) //注解作用级别:运行时
@interface myAnnotations2 {
//如果只有一个值,建议用value,使用时可以省略value
String value();
}
2 反射
java Reflection 反射机制允许程序在执行期间借助于Refection API 取得任何类的内部信息,并能直接操作任意对象的内部属性及方法。
2.1 获得class类的几种方式
public class TestClass {
public static void main(String[] args) {
com.demo9.Person person=new student();
//方式1 通过对象获得
Class c1=person.getClass();
System.out.println(c1);
//方式二:通过forname获得
try {
Class c2=Class.forName("com.demo9.student");
System.out.println(c2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//方式3 通过类名获得
Class c3=student.class;
System.out.println(c3);
//方式4 基本内置类的类型中有一个类型属性
Class<Integer> c4=Integer.TYPE;
System.out.println(c4);
}
}
2.2 通过反射创造对象
public class Test3 {
public static void main(String[] args) {
try {
Class aClass = Class.forName("com.demo9.User");
//构造一个对象,通过无参构造器
User user=(User) aClass.newInstance();
System.out.println(user);
//通过构造器创建对象
Constructor constructor = aClass.getDeclaredConstructor(String.class, int.class);
User instance = (User)constructor.newInstance("ding", 001);
System.out.println(instance);
//通过反射获取一个方法
Method method = aClass.getDeclaredMethod("setName", String.class);
method.invoke(user,"ding");
System.out.println(user);
//通过反射操作属性
User user2=(User) aClass.newInstance();
Field field = aClass.getDeclaredField("name");
//私有属性,无法直接操作,需要先关闭
field.setAccessible(true);
field.set(user2,"002");
System.out.println(user2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.3 通过反射获得注解
package com.demo9;
import java.lang.annotation.*;
import java.lang.reflect.Field;
/**
* @PackageName: com.demo9
* @ClassName: Test5
* @author:
* @date:
* @Description: 反射操作注解
*/
@table1("db_student")
class AAA{
@Field01(columnName = "db_name",type = "varchar",length = 10)
String name;
@Field01(columnName = "db_id",type = "int",length = 10)
int id;
@Field01(columnName = "db_age",type = "int",length = 10)
int age;
public AAA(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public AAA() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface table1{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Field01{
String columnName();
String type();
int length();
}
public class Test5 {
public static void main(String[] args) {
try {
Class aClass = Class.forName("com.demo9.AAA");
System.out.println(aClass);
//通过反射获得类注解
Annotation[] annotations=aClass.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
table1 table01=(table1)annotation;
System.out.println(table01.value());
}
//通过反射获得属性注解
Field field = aClass.getDeclaredField("name");
Field01 annotatopn=field.getAnnotation(Field01.class);
System.out.println(annotatopn.columnName());
System.out.println(annotatopn.type());
System.out.println(annotatopn.length());
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文作者:发呆鱼
本文链接:https://www.cnblogs.com/dyiblog/articles/15788162.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步