当前位置:网站首页>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()); 边栏推荐
- 皮质-皮质网络的多尺度交流
- 那些事情是用Unity开发项目应该一开始规划好的?如何避免后期酿成巨坑?
- Pinduoduo API interface (attach my available API)
- A used in the study of EEG ultra scanning analysis process
- Cobbleland 博览会 基础系列 1
- 第一个C函数:如何实现板级初始化?
- How Unity handles C# under the hood
- 技术分享 | 实战演练接口自动化如何处理 Form 请求?
- 姿态解算-陀螺仪+欧拉法
- unable to extend table xxx by 1024 in tablespace xxxx
猜你喜欢
随机推荐
Amazon Get AMAZON Product Details API Return Value Description
进制转换间的那点事
Monte Carlo
技术分享 | 实战演练接口自动化如何处理 Form 请求?
unable to extend table xxx by 1024 in tablespace xxxx
淘宝sku API 接口(PHP示例)
prometheus学习4Grafana监控mysql&blackbox了解
《猪猪1984》NFT 作品集将上线 The Sandbox 市场平台
NFT 的价值从何而来
Waldom Electronics宣布成立顾问委员会
[损失函数]——均方差
How Unity handles C# under the hood
损失函数——负对数似然
MySQL 版本升级心得
Douyin API interface
已解决EROR 1064 (42000): You have an error in. your SOL syntax. check the manual that corresponds to yo
一个小时快速熟悉MySQL基本用法
使用Keras构建GAN,以Mnist为例
每日sql -用户两天留存率
《Show and Tell: A Neural Image Caption Generator》论文解读









