当前位置:网站首页>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 :
边栏推荐
猜你喜欢
对象的序列化
一个新的微型ORM开源框架
Palindrome (csp-s-2021-palin) solution
浅谈JVM(面试常考)
[to be continued] [depth first search] 547 Number of provinces
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Web APIs DOM节点
Romance of programmers on Valentine's Day
Chapter 6 data flow modeling - after class exercises
Sword finger offer 04 Search in two-dimensional array
随机推荐
Solon 框架如何方便获取每个请求的响应时间?
YOLOv5添加注意力机制
读者写者模型
Haut OJ 1321: mode problem of choice sister
PMP candidates, please check the precautions for PMP examination in July
Detailed explanation of expression (csp-j 2021 expr) topic
数仓项目的集群脚本
[binary search] 69 Square root of X
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
The number of enclaves
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
Codeforces Round #715 (Div. 2) D. Binary Literature
room数据库的使用
剑指 Offer 05. 替换空格
Pointnet++学习
Haut OJ 1245: large factorial of CDs --- high precision factorial
MySQL数据库(一)
[turn]: Apache Felix framework configuration properties
Developing desktop applications with electron
使用Electron开发桌面应用