当前位置:网站首页>Notes learning summary
Notes learning summary
2022-06-27 14:11:00 【Where is the painting boat tonight】
1. annotation
1.1 What is annotation
Annotation yes JDK1.5 New technology introduced after
1.2 The role of annotations
Not the program itself , The procedure can be explained ( This point is consistent with the notes (comment) No difference )
It can be read by other programs ( Such as compiler )
2. Built-in annotations
2.1Override
It's defined in java.lang.Override in , This note applies only to rhetorical devices , Indicates that a method intends to override another method declaration in the superclass
2.2Deprecated
When a type or type member uses @Deprecated Embellished words , The compiler will not encourage the use of this tagged program element .
2.3SuppressWarnings
Used to suppress compile time warnings
package com.lixi;
// The following three annotations are built-in annotations
// Suppression warning , It is not recommended to use more
@SuppressWarnings("all")
public class Test01 {
// Rewritten notes
@Override
public String toString() {
return super.toString();
}
// Programmers are not recommended to use ,
@Deprecated
public void test() {
System.out.println("Deprecated");
}
public static void main(String[] args) {
Test01 t = new Test01();
t.test();
}
}
3. Yuan notes
The role of meta annotation is to annotate other annotations
3.1 Target
Simply put, it means where the described annotation can be used
● ElementType.CONSTRUCTOR: Used to describe a constructor
● ElementType.FIELD: Member variables 、 object 、 attribute ( Include enum example )
● ElementType.LOCAL_VARIABLE: Used to describe local variables
● ElementType.METHOD: Used to describe a method
● ElementType.PACKAGE: Used to describe a package
● ElementType.PARAMETER: Used to describe parameters
● ElementType.TYPE: Used to describe a class 、 Interface ( Including annotation types ) or enum Statement
3.2Retention
Used to describe the life cycle of annotations
● RetentionPolicy.SOURCE : Discard... At compile time . These annotations don't make any sense after compilation , So they don't write bytecode [email protected], @SuppressWarnings All belong to this kind of annotation .
● RetentionPolicy.CLASS : Discard... When the class is loaded . Useful in bytecode file processing . Annotations default to this way
● RetentionPolicy.RUNTIME : Never throw away , The runtime also keeps the comment , So you can use the reflection mechanism to read the annotation information . Our custom annotations usually use this way .
3.3Documented
A simple Annotations Mark notes , Indicates whether to add annotation information to java In the document .
3.4Inherited
Subclasses can inherit comments from their parents
package com.lixi;
import java.lang.annotation.*;
@SuppressWarnings("all")
@MyAnnotation
public class Test02 {
// Test meta annotation , Focus on mastering Target,Retention
@MyAnnotation
public void Demo() {
}
}
// Define an annotation
// Target It indicates where our custom annotations can be used
@Target(value = {ElementType.METHOD, ElementType.TYPE})
// Retention It indicates when our annotations are still valid ,RUNTIME > CLASS > SOURCE
@Retention(value = RetentionPolicy.RUNTIME)
// Documented Indicates whether to generate our annotation in JAVAdoc in
@Documented
// Inherited Subclasses can inherit comments from their parents
@Inherited
@interface MyAnnotation {
}
4. Custom annotation
Be careful
1.Annotation Type is defined as @interface, be-all Annotation Will automatically inherit java.lang.Annotation This interface , And you can't inherit other classes or interfaces .
2. Parameter members can only use public Or default (default) These two access modifications .
3. The name of the method is the name of the parameter
4. The return type is the parameter type ( The return type can only be a basic type String,enum,Class)
5. Can pass default To declare the default value of the parameter
6. If there is only one parameter member , We generally default to value
package com.lixi;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@SuppressWarnings("all")
public class Test03 {
@Annotation2(schools = {" university "},id=-1)
public void Demo(){
}
@Annotation3("jili")
public void Demo1(){
}
}
@SuppressWarnings("all")
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation2 {
// Annotated parameters : Parameter type + Parameter name + ()
//default Indicates the default value of the annotation , With default values , You don't have to assign values to annotations
String value() default "jili";
int count () default 0;
int id () ;
String[] schools() ;
}
@SuppressWarnings("all")
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation3 {
// A single parameter uses value, You don't have to write value
String value();
}General annotations do not have so many parameters , It's just a demonstration
边栏推荐
猜你喜欢
![[XMAN2018排位赛]通行证](/img/eb/7bf04941a96e9522e2b93859266cf2.png)
[XMAN2018排位赛]通行证

Implementing springboard agent through SSH port forwarding configuration

NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Principle Comparison and analysis of mechanical hard disk and SSD solid state disk

国产数据库乱象

ENSP cloud configuration

Openssf security plan: SBOM will drive software supply chain security
![[WUSTCTF2020]girlfriend](/img/a8/33fe5feb7bcbb73ba26a94d226cc4d.png)
[WUSTCTF2020]girlfriend

Privacy computing fat offline prediction

Calcul de la confidentialité Fate - Prévisions hors ligne
随机推荐
CCID Consulting released the database Market Research Report on key application fields during the "14th five year plan" (attached with download)
Domestic database disorder
芯片供给过剩之际,进口最多的中国继续减少进口,美国芯片慌了
SFINAE
Rereading the classic: the craft of research (1)
隱私計算FATE-離線預測
Summary and Thinking on interface test automation
Pytoch learning 2 (CNN)
AXI总线
How to select cross-border e-commerce multi merchant system
How to use 200 lines of code to implement Scala's Object Converter
enable_ if
[安洵杯 2019]Attack
初识云原生安全:云时代的最佳保障
现在开户有优惠吗?网上开户是否安全么?
Can flush open an account for stock trading? Is it safe?
深入理解位运算
PCL库——报错解决:安装时遇到的cmake与anaconda的冲突问题
Kyndryl与Oracle和Veritas达成合作
crane:字典项与关联数据处理的新思路