当前位置:网站首页>Annotation and reflection
Annotation and reflection
2022-07-05 05:30:00 【Drunken dragon boat】
Comments and reflections
One : annotation
Custom annotation :
import java.lang.annotation.*;
/** * Built-in annotations * @Override Representation override * @Deprecated Indicates that it has been deprecated * @SuppressWarnings Indicates no warning */
/** * Yuan notes - Custom annotation * @author liu ming yong */
@MyAnnotation(value = " Custom annotation ")
public class Annotation {
// Note if the parameter has no default value , Then the annotation must be assigned
@MyAnnotation(value = "qq")
public void s(){
}
}
/** * @Target Indicates where annotations can be used TYPE( class ),METHOD( Method ) * @Retention Indicates where the annotation is valid runtime( Runtime )>class( Compiled bytecode )>sources( Source code ) * @Documented Indicates whether the annotation is produced in JAVAdoc in * @Inherited Indicates that the child class can inherit the parent class annotation * @author liu ming yong */
@Target(value = {
ElementType.TYPE,ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface MyAnnotation {
// Annotated parameters : Parameter type Parameter name ();
String value();
String name() default "my name is liu ming yong";
int age() default 0;
String[] schools() default {
" National Defense University "," Tsinghua University "};
}
Two : Reflection
The concept of reflection :
Reflection( Reflection ) yes java It is regarded as a quasi dynamic language ( A language that can change its structure during operation ) The key to , Reflection allows the program to get the internal information of any class during execution , And can directly operate the internal properties or methods of any object .
After class loading , In the heap memory method area, a Class Type object ( There is only one class Class object ), This object contains the complete class structure information .
Get reflection Class In several ways :
/** * @author liu ming yong */
public class Reflection01 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
// Mode one : Get... By object
Class c1 = person.getClass();
System.out.println(c1);
// Mode two : adopt forName get
Class c2 = Class.forName(" Comments and reflections .Student");
System.out.println(c2);
// Mode three : By class name .class get
Class c3 = Student.class;
System.out.println(c3);
// Mode 4 : Get through the wrapper class of basic data type
Class c4 = Integer.TYPE;
System.out.println(c4);
// Get parent type
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 {
}
Get the structure of the class runtime :
边栏推荐
- 第六章 数据流建模—课后习题
- Pointnet++的改进
- ssh免密登录设置及使用脚本进行ssh登录并执行指令
- 26、 File system API (device sharing between applications; directory and file API)
- 记录QT内存泄漏的一种问题和解决方案
- 剑指 Offer 35.复杂链表的复制
- Solon Logging 插件的添加器级别控制和日志器的级别控制
- Haut OJ 1221: a tired day
- 【Jailhouse 文章】Look Mum, no VM Exits
- Sword finger offer 53 - I. find the number I in the sorted array
猜你喜欢
Gbase database helps the development of digital finance in the Bay Area
Fragment addition failed error lookup
剑指 Offer 05. 替换空格
Little known skills of Task Manager
[转]MySQL操作实战(一):关键字 & 函数
Service fusing hystrix
Palindrome (csp-s-2021-palin) solution
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
读者写者模型
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
随机推荐
剑指 Offer 09. 用两个栈实现队列
kubeadm系列-00-overview
A misunderstanding about the console window
Sword finger offer 53 - I. find the number I in the sorted array
Pointnet++ learning
Solon 框架如何方便获取每个请求的响应时间?
2022上半年全国教师资格证下
Introduction to memory layout of FVP and Juno platforms
Haut OJ 1221: a tired day
[depth first search] 695 Maximum area of the island
[speed pointer] 142 circular linked list II
剑指 Offer 05. 替换空格
On-off and on-off of quality system construction
[trans]: spécification osgi
每日一题-无重复字符的最长子串
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
[to be continued] [UE4 notes] L3 import resources and project migration
Haut OJ 1321: mode problem of choice sister
YOLOv5添加注意力機制
Haut OJ 1352: string of choice