当前位置:网站首页>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 :
边栏推荐
- [转]MySQL操作实战(三):表联结
- Software test -- 0 sequence
- Under the national teacher qualification certificate in the first half of 2022
- [speed pointer] 142 circular linked list II
- kubeadm系列-01-preflight究竟有多少check
- Reflection summary of Haut OJ freshmen on Wednesday
- A misunderstanding about the console window
- Haut OJ 1350: choice sends candy
- Csp-j-2020-excellent split multiple solutions
- Detailed explanation of expression (csp-j 2021 expr) topic
猜你喜欢
Double pointer Foundation
剑指 Offer 53 - II. 0~n-1中缺失的数字
剑指 Offer 53 - I. 在排序数组中查找数字 I
第六章 数据流建模—课后习题
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
[to be continued] [UE4 notes] L3 import resources and project migration
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
Service fusing hystrix
剑指 Offer 58 - II. 左旋转字符串
随机推荐
用STM32点个灯
How can the Solon framework easily obtain the response time of each request?
Introduction to tools in TF-A
Web APIs DOM节点
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
ALU逻辑运算单元
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Web APIs DOM node
Solon Logging 插件的添加器级别控制和日志器的级别控制
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Reflection summary of Haut OJ freshmen on Wednesday
Binary search basis
SSH password free login settings and use scripts to SSH login and execute instructions
Pointnet++的改进
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
SDEI初探-透过事务看本质
[turn]: Apache Felix framework configuration properties
kubeadm系列-02-kubelet的配置和启动
Programmers' experience of delivering takeout
过拟合与正则化