当前位置:网站首页>注解和反射
注解和反射
2022-07-26 14:03:00 【看小虫子】
注解的定义,元注解
@override :定义在java.lang.Override 中,此注释只用于修辞方法,表示一个方法声明大算重写超类中的另一个方法声明;
@Deprecated: 定义在java.lang.Deperecated中,此注释可用于修辞手法,属性,类表是不建议使用这样的元素
@ SuppressWarnings:lang包下,用来抑制编译时的警告信息;
元注解
定义:用来注解其他注解,java定义了4个标准的meta-annotation 他们被用来提供对其他注解做说明
@Target :用于描述注解的使用范围
@Retention:表示需要在什么急别保存该注释信息,用于描述注解的生命周期
@Document :说明该注解将被包含在JAVADoc中
@inherited:说明子类可以继承父类中的该注解
自定义注解
使用@interface自定义注解时,自动继承java.lang.annotation.Annotation
package TestaAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Test01 {
//注解可以显示赋值,如果没有默认值,我们就必须给注解赋值
@MyAnnotation2("yan")
public void test2(){
}
@MyAnnotation(age = 18)
public void test(){
}
}
@Target({
ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
//注解的参数:参数类型+参数名();
String name() default "";
int age();
int id() default -1;//默认值为-1代表不存在
String[] school() default {
"我家","他家"};
}
@Target({
ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
String value();//value可以省略
}
java反射优点和缺点
优点:可以实现动态创建和编译,体现很大的灵活性
缺点:对性能有影响,使用反射基本上是一种解释操作,我们可以高数jvm,我们希望做什么并且它满足我们的要求,这类操作总是慢于直接执行相同的操作。
package TestReflected;
//什么叫反射
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException {
//通过反射获取类的Class对像 (class小写为类,C大写是对象)
Class C1 = Class.forName("TestaAnnotation.Test01");
System.out.println(C1);
//一个类在内存中只有一个Class对象
//一个类被加载后,类的整个结构都会被封装在Class对象中。
}
}
//实体类 只有一些属性pojo,entity
class User{
private String name;
private int id;
private int age;
public User() {
}
public User(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
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 "User{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
边栏推荐
- Disease knowledge discovery based on spo semantic triples
- Docker container MySQL enables binlog and scheduled backup
- php使用sqlserver
- 基于双层主题模型的技术演化分析框架及其应用
- Inspiration from brain: introduction to synaptic integration principle in deep neural network optimization
- DP sword finger offer II 100. sum of minimum paths in triangle
- 向路由组件传递参数
- .net6 encounter with the League of heroes - create a game assistant according to the official LCU API
- Synchronization mechanism of go (sync.mutex)
- C language_ Structure pointer variable introduction
猜你喜欢

Pytorch学习笔记(一)安装与常用函数的使用

@千行百业,一起乘云而上!

Jenkins 中 shell 脚本执行失败却不自行退出

OA项目之我的会议

Circular queue (implemented in C language)

JS page turning, kkpager.js page turning

12437 words, take you to explore the principle of RPC communication

LCL three-phase PWM rectifier (inverter)

基于机器学习的技术术语识别研究综述

Why does WPS refuse advertising?
随机推荐
[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]
Force deduction ----- the number of words in the string
Add a display horizontal line between idea methods
【数学建模】常用基本模型总结
TDSQL-C Serverless:助力初创企业实现降本增效
IDEA(warning)No artifacts configured
Pytorch学习笔记(三)模型的使用、修改、训练(CPU/GPU)及验证
Research on Chinese medicine assisted diagnosis and treatment scheme integrating multiple natural language processing tasks -- taking diabetes as an example
mysql5.7通过文件zip方式安装-九五小庞
大脑带来的启发:深度神经网络优化中突触整合原理介绍
向路由组件传递参数
Basic knowledge about memory chips
.net6与英雄联盟邂逅之——根据官方LCU API制作游戏助手
Pytorch学习笔记(一)安装与常用函数的使用
【着色器实现Overlay重新覆盖变装效果_Shader效果第九篇】
敏捷开发与DevOps的对比
[GYCTF2020]FlaskApp
Detailed explanation of alter field of MySQL Foundation
With frequent data leakage and deletion events, how should enterprises build a security defense line?
Basic syntax of MySQL DDL and DML and DQL