当前位置:网站首页>注解与反射
注解与反射
2022-07-05 05:23:00 【醉卧雕龙舫 、】
一:注解
自定义注解:
import java.lang.annotation.*;
/** * 内置注解 * @Override 表示重写 * @Deprecated 表示已经弃用 * @SuppressWarnings 表示不提示警告 */
/** * 元注解-自定义注解 * @author liu ming yong */
@MyAnnotation(value = "自定义注解")
public class Annotation {
// 注解如果参数没有默认值,则必须给注解赋值
@MyAnnotation(value = "qq")
public void s(){
}
}
/** * @Target 表示注解可以用在什么地方 TYPE(类),METHOD(方法) * @Retention 表示注解在什么地方有效 runtime(运行时)>class(编译成的字节码)>sources(源码) * @Documented 表示是否将注解生产在JAVAdoc中 * @Inherited 表示子类可以继承父类的注解 * @author liu ming yong */
@Target(value = {
ElementType.TYPE,ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface MyAnnotation {
// 注解的参数:参数类型 参数名();
String value();
String name() default "my name is liu ming yong";
int age() default 0;
String[] schools() default {
"国防大学","清华大学"};
}
二:反射
反射概念:
Reflection(反射)是java被视为准动态语言(在运行期间可改变结构的语言)的关键,反射允许程序在执行期间获取到任何类的内部信息,并可直接操作任意对象的内部属性或方法。
类加载完成后,在堆内存方法区中就产生了一个Class类型对象(一个类只有一个Class对象),这个对象就包含了完整的类的结构信息。
获得反射Class的几种方式:
/** * @author liu ming yong */
public class Reflection01 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
// 方式一:通过对象获得
Class c1 = person.getClass();
System.out.println(c1);
// 方式二:通过forName获得
Class c2 = Class.forName("注解与反射.Student");
System.out.println(c2);
// 方式三:通过类名.class获得
Class c3 = Student.class;
System.out.println(c3);
// 方式四:通过基本数据类型的包装类获取
Class c4 = Integer.TYPE;
System.out.println(c4);
// 获取父类类型
Class c5 = c1.getSuperclass();
System.out.println(c5);
}
}
class Person {
String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
class Student extends Person {
}
获取类运行时的结构:
边栏推荐
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- 2022年上半年国家教师资格证考试
- Yolov5 ajouter un mécanisme d'attention
- Double pointer Foundation
- 服务熔断 Hystrix
- Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
- Under the national teacher qualification certificate in the first half of 2022
- SAP-修改系统表数据的方法
- [转]MySQL操作实战(三):表联结
- Use of snippets in vscode (code template)
猜你喜欢

To the distance we have been looking for -- film review of "flying house journey"
![[to be continued] [UE4 notes] L3 import resources and project migration](/img/81/6f75f8fbe60e037b45db2037d87bcf.jpg)
[to be continued] [UE4 notes] L3 import resources and project migration

《动手学深度学习》学习笔记

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[转]: OSGI规范 深入浅出

嵌入式数据库开发编程(零)

2022年上半年国家教师资格证考试
![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)
[interval problem] 435 Non overlapping interval

小程序直播+電商,想做新零售電商就用它吧!

Yolov5 adds attention mechanism
随机推荐
[to be continued] [UE4 notes] L1 create and configure items
BUUCTF MISC
A preliminary study of sdei - see the essence through transactions
质量体系建设之路的分分合合
Haut OJ 2021 freshmen week II reflection summary
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
Yolov5 ajouter un mécanisme d'attention
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Do a small pressure test with JMeter tool
[转]MySQL操作实战(三):表联结
Haut OJ 1357: lunch question (I) -- high precision multiplication
The next key of win generates the timestamp file of the current day
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Magnifying glass effect
2022年上半年国家教师资格证考试
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Haut OJ 1243: simple mathematical problems
C language Essay 1
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?