当前位置:网站首页>2022-08-10 The sixth group Hiding spring study notes
2022-08-10 The sixth group Hiding spring study notes
2022-08-11 03:29:00 【Hot shin ramen】
活动地址:CSDN21天学习挑战赛
前言
Annotate today、反射的学习
JavaMost of the basic courses have been completed,Some small points left to add
️注解
注解:Annotation JDK1.5
public @interface MyAnnotation(){
}
Create your own annotations
Annotations also have annotations,元注解:Annotation added specifically to annotations
Retentionetc. are enumerations,后面跟的是常量
We found that annotations can have methods,可以有默认值
String name() default “Tom”;
定义方法格式:String name(); Do not write the default value, you need to assign,with default valuedefault指定默认值
public class Ch01{
@MyAnnotation(age=20) It can be understood as adding a return value
public void show(){
}
}
If there is only one, you don't need to write the name,If there are more than one need to write the name
在javain the entire annotation system,There are three very important trunk classes
1.Annotation 接口 Define some common methods
2.ElementType枚举 Used to specify the type of annotation (where to use annotations)
3.RetentionPolicy 枚举 The strategy used to specify annotations,Different types of policies specify different scopes
(1)SUTCE Annotations exist only during compiler processing,After compile time processing,这个注解就没有了
(2)CLASS 注解在.classstill valid in the file
(3)RUNTIME compile time doesn't work,only during runtimeJVM读取
Java自带注解
️作用在代码上的注解
[email protected],Check if a method is an overridden method,If it returns its parent class,Or if there is no such method in the referenced interface, an error will be reported
[email protected] Marked method is obsolete
[email protected] Tell the editor to ignore warnings
[email protected] Ignore any parameters used as generic variable method or constructor of warning
[email protected] Identifies an interface as a functional interface
[email protected] Identifies that an annotation can be used multiple times on the same declaration
all:忽略所有警告
boxing:Ignore boxing and unboxing warnings
rawtypes:Use generate without specifying data type
unchecked:Ignore warnings without type checking
unused:Ignore unused warnings
️元注解
[email protected] Identifies the scope of this annotation
[email protected] Marks whether this annotation is included in the user documentation
[email protected] What information can this annotation modify
[email protected] If a class uses this annotation ,那么其子类也会继承这个注解
️反射
Class类
You can get all the structures in the class,You can access class information without manipulating the class
1.直接使用类名.class
2.使用全类名
3.使用对象
Class<>When using generics, add
获取类的名字:clazz.getName()
获取类的加载器:
获取资源
URL resource = clazz.getResource(“”);
获取父类
clazz.getSuperclass()
Is it an array of interfaces?
clazz.isArray();
clazz.isInterface();
重点
Dog dog = clazz.newInstance(); jdk9过时
创建出dog一个对象:使用classclass object instantiates an object
clazz.getDeclaredConstructor().newInstance(); 代替 In the constructor method
对成员变量的操作:
对成员变量进行操作,只能获取到public修饰的属性
可以获取到private属性
get all properties
获取对象的属性
String str = (String)type.get(dog1); If you want a string, use coercion
给对象的属性赋值
Privatized things can also operate
反射调用方法???
通过反射来调用构造器
通过构造器创建对象
构造器私有化
sout(dog1.getName());
对注解的操作
️总结
Although reflection and annotation are not as difficult as multi-threading and other sections,But the importance cannot be ignored,For object creation we encourage the use of reflection to create,它允许程序创建和控制任何类的对象(根据安全性限制),无需提前硬编码目标类.这些特性使得反射特别适用于创建以非常普通的方式与对象协作的库.
业精于勤,荒于嬉; 行成于思,毁于随.
边栏推荐
猜你喜欢
Is Redis old?Performance comparison between Redis and Dragonfly
互换性与测量技术-公差原则与选用方法
Getting Started with Raspberry Pi (5) System Backup
QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
I didn't expect MySQL to ask these...
你不知道的 console.log 替代品
Summary of debugging skills
The most unlucky and the luckiest
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
Google search skills - programmer is recommended
随机推荐
Watch to monitor
Google search skills - programmer is recommended
Kubernetes集群搭建Zabbix监控平台
What kind of programming trading strategy types can be divided into?
this question in js
80端口和443端口是什么?有什么区别?
Idea (优选)cherry-pick操作
重庆纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
云平台下ESB产品开发步骤说明
浅析一下期货程序化交易好还是手工单好?
【Pdf自动生成书签】
Economic Misunderstandings in the Crypto World: Is Cash a Savings?Scarcity creates value?
E-commerce project - mall time-limited seckill function system
The thirteenth day of learning programming
元素的BFC属性
FTP错误代码列表
高校就业管理系统设计与实现
App基本框架搭建丨日志管理 - KLog
uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
[Pdf generated automatically bookmarks]