当前位置:网站首页>2022-08-10 Group 4 Self-cultivation class study notes (every day)
2022-08-10 Group 4 Self-cultivation class study notes (every day)
2022-08-11 07:29:00 【bodybuilding class】
Annotations and reflections
Annotation: Annotation, Java annotation, a mechanism introduced by JDK5.
Classes, methods, variables, parameters, and packages in Java can be marked
Meta-annotations: annotations specially added to annotations
We found that there can be methods in annotations,
1. The format of the definition method: String name();
2. There can be a default value or not. If there is no default value, you must fill in the corresponding value when using it.
If you need a default value, usedefault specifies the default value.
3. If you want to use it without specifying a specific name,
If you don't learn reflection, annotations are useless!!!
In the entire annotation system of java, there are 3 very important backbone classes,
1. Annotation interface, which defines some commonly used methods
2. ElementType enumeration
It is used to specify annotationstype.Speaking of people, where should I use my annotations???
3. RetentionPolicy enumeration
It is used to specify the annotation policy.The scope of annotations specified by different types of policies is different.
(1) SOURCE, the annotation only exists during the compiler processing. After the compilation is processed, the annotation is useless.
(2) CLASS, the annotation is still valid in the .class file.
(3) RUNTIME, it does not work at compile time, it is read by JVM only at run time.
Java's own annotations, 10.4 annotations java.lang.annotation
6 annotations in java.lang
Annotations that act on the code
1. @Override, check whether the method is an overriding method.If the parent class is returned, or there is no such method in the referenced interface, an error will be reported
2. @Deprecated, marked method, obsolete method.
3. @SuppressWarnings editor to ignore warnings
4. @SafeVarargs, JDK7 supports ignoring any warnings generated by methods or constructors that use parameters as generic variables
5. @FunctionalInterface, JDK8 starts to support,Indicates that an interface is a functional interface
6. @Repeatable, which is supported by JDK8, indicates that an annotation can be used multiple times on the same declaration
all: ignore all warnings
boxing: ignore boxing and unpackingBox warnings
rawtypes: use generation without specifying data types
unchecked: ignore warnings without type checking
unused: ignore warnings when unused
meta-annotations:
1. @Retention: Identifies the scope of this annotation
2. @Documented: Marks whether this annotation is included in the user document
3. @Target: What information can be modified by this annotation
4. @Inherited: If a class usesWith the @Inherited annotation, its subclasses will also inherit this annotation
Create objects using reflection
Several ways to create objects with reflection
/*Get the bytecode of a class*/Class pClass = Class.forName("com.bjsxt.demo1.Person");/** Get the constructor of the class* */Constructor cons = pClass.getDeclaredConstructor(int.class, String.class, String.class);/** Let the constructor execute* */Person person=(Person) cons.newInstance(1,"Xiao Ming","Male");System.out.println(person);/** An object of this class can be instantiated directly with bytecode* Bytecode.newInstance() is actually calling the parameterless constructor of the class to instantiate an object* If you use this method, you must ensure that there must be an empty parameter constructor in the class* */Person person2 = (Person)pClass.newInstance();System.out.println(person2)
Reflecting to assign values to object properties
/*Get the bytecode of a class*/Class pClass = Class.forName("com.bjsxt.demo1.Person");Person person = (Person)pClass.newInstance();System.out.println(person);/** Complete the assignment of object properties through the field object* */Field id = pClass.getDeclaredField("id");Field lastname = pClass.getDeclaredField("lastname");Field gender = pClass.getDeclaredField("gender");Field firstname = pClass.getDeclaredField("firstname");// Private modified properties cannot be accessed directly if you want to use the properties firstid.setAccessible(true);lastname.setAccessible(true);gender.setAccessible(true);firstname.setAccessible(true);// assign a value to the propertyid.set(person,1);lastname.set(person,"Xiao Ming");gender.set(person,"male");// Static member variables can be assigned without an objectfirstname.set(null,"king");System.out.println(person)
Reflection call object method
/*Get the bytecode of a class*/Class pClass = Class.forName("com.bjsxt.demo1.Person");Constructor cons = pClass.getDeclaredConstructor(int.class, String.class, String.class);Person person = (Person)cons.newInstance(1, "Xiao Ming", "Male");// 1 Execute a method with no parameters and no return value// Get the Method object of the method to callMethod showNameMethod = pClass.getDeclaredMethod("showName");// Let the method call the invoke method, which means let the current method execute// If it is an instance method, an object must be required when the method is executed// If the method requires parameters to execute, then the actual parameters are also passed inshowNameMethod.invoke(person);// 2 If executing a method with parameters and return value// Then you need to pass in the actual parameters when callingMethod sumMethod = pClass.getDeclaredMethod("sum", int.class, double.class);// It can be accessed when setting the method, so that the method is private and the method is not available.sumMethod.setAccessible(true);double res=(double)sumMethod.invoke(person,1,3.14);System.out.println(res);// 3 execute a static method// Static methods can be called with objects or with class names// So you can not pass in the object when executing the static methodMethod setFirstname = pClass.getDeclaredMethod("setFirstname", String.class);setFirstname.invoke(null,"Li");System.out.println(person);
Violent Injection
color.setAccessible(true);color.set(dog,"black");System.out.println(dog.getColor());
Singleton Pattern
Constructor Privatization
// singleton mode// 1. Constructor privatizationConstructor declaredConstructor1 = clazz.getDeclaredConstructor(String.class);declaredConstructor1.setAccessible(true);Dog dog1 = declaredConstructor1.newInstance("Xiaoqiang");System.out.println(dog1.getName());
边栏推荐
- Internet phone software or consolidation of attack must be "free" calls security clearance
- 快速了解集成学习
- 每日sql-找到每个学校gpa最低的同学(开窗)
- Daily SQL - find each of the students school gpa minimum (window)
- golang fork 进程的三种方式
- pytorch,numpy两种方法实现nms类间+类内
- 【@网络工程师:用好这6款工具,让你的工作效率大翻倍!】
- 淘宝API常用接口与获取方式
- 图的拉普拉斯矩阵
- Daily sql-statistics of the number of professionals (including the number of professionals is 0)
猜你喜欢
随机推荐
每日sql-找到每个学校gpa最低的同学(开窗)
一个小时快速熟悉MySQL基本用法
抖音获取douyin分享口令url API 返回值说明
EasyPlayer针对H.265视频不自动播放设置下,loading状态无法消失的解决办法
Amazon API interface Daquan
淘宝sku API 接口(PHP示例)
皮质-皮质网络的多尺度交流
Douyin API interface
LeetCode刷题系列 -- 46. 全排列
unable to extend table xxx by 1024 in tablespace xxxx
下一代 无线局域网--强健性
1688 product interface
【软件测试】(北京)字节跳动科技有限公司二面笔试题
什么是Inductive learning和Transductive learning
每日sql-员工奖金过滤和回答率排序第一
A used in the study of EEG ultra scanning analysis process
Discourse 的关闭主题(Close Topic )和重新开放主题
Tidb二进制集群搭建
How do you optimize the performance of your Unity project?
ROS 服务通信理论模型