当前位置:网站首页>Enumeration and annotation
Enumeration and annotation
2022-07-27 21:41:00 【Xiao Tang learns to catch babies】
== = = = = = = = = Enumerations and annotations = = = = = = = = = = ==
== = = = = = = = = = enumeration = = = = = = = = = = ==
One 、 Two implementations of enumeration
- Custom class implements enumeration
- Use enum Keyword to implement enumeration
1、 Custom enumeration class
1. There is no need to provide setXxx Method , Because enumeration object values are usually read-only .
2. For enumerating objects / Attributes use final + static Co modification , Implement the underlying optimization .
3. Enumeration object names are usually all uppercase , Naming conventions for constants .
4. Enumerate objects as needed , There can also be multiple attributes
class Season{ class
private String name;
private String desc;// describe
// Four objects are defined , Fix .
public static final Season SPRING=new Season(“ spring ”, “ warm ”);
public static final Season WINTER=new Season(“ winter ”, “ cold ”);
public static final Season AUTUMN =new Season(“ autumn ”, “ cool ”);
public static final Season SUMMER new Season(“ summer ”, “ scorching hot ”);
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc(){
return desc;
}
@Override
public String toString() {
return “Season{” +
“name=’” + name + ‘’’ +
“, desc=’” + desc + ‘’’ + ‘}’;
}
}
Summary : Carry out custom class implementation enumeration , It has the following characteristics :
1) Constructor privatization
2) This class creates a set of objects [ Four spring, summer, autumn and winter ]
3) Exposed objects ( By adding public final static Modifier )
4) Can provide get Method , But don't offer set
2、enum Keyword to implement enumeration
public class Enumeration03 {
public static void main(String[] args) { System.out.println(Season2.AUTUMN);
System.out.println(Season2.SUMMER); } }
// Demonstrate the use of enum Keyword to implement enumeration classes
enum Season2 {// class
// Four objects are defined , Fix .
// public static final Season SPRING = new Season(“ spring ”, “ warm ”);
// public static final Season WINTER = new Season(“ winter ”, “ cold ”);
// public static final Season AUTUMN = new Season(“ autumn ”, “ cool ”);
// public static final Season SUMMER = new Season(“ summer ”, “ scorching hot ”);
// If used enum To implement enumeration classes
//1. Use keywords enum replace class
//2. public static final Season SPRING = new Season(“ spring ”, “ warm ”) Use it directly // SPRING(“ spring ”, “ warm ”) Reading Constant names ( Argument list )
//3. If there are multiple constants ( object ), Use , No. interval is enough
//4. If you use enum To implement enumeration , Requires that a constant object be defined , Write it at the front
//5. If we use a parameterless constructor , Create constant objects , You can omit () SPRING(“ spring ”, “ warm ”), WINTER(“ winter ”, “ cold ”), AUTUMN(“ autumn ”, “ cool ”), SUMMER(“ summer ”, “ scorching hot ”)/, What()/;
private String name;
private String desc;
// describe private Season2() { // Parameterless constructors
}
private Season2(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return “Season{” + “name=’”
- name + ‘’’ + “, desc=’”
- desc + ‘’’ + ‘}’;
- }
- }
enum Keyword implementation enumeration considerations
- When we use enum Keyword when developing an enumeration class , Default will inherit Enum class , And it's a final class [ How to prove ], Teachers use javap work To demonstrate
- Conventional public static final Season2 SPRING = new Season2(“ spring ”, “ warm ”); reduce to SPRING(“ spring ”, “ warm ”), Here must You have to know , Which constructor it calls .
- If you use a parameterless constructor establish enumerable object , The argument list and parentheses can be omitted
- When there are multiple enumerated objects , Use , interval , There is a semicolon at the end
- The enumeration object must be placed at the beginning of the line of the enumeration class .
== = = = = = = = = = annotation = = = = = = = = = = ==
One 、 The understanding of annotation
- annotation (Annotation) Also known as metadata (Metadata), Used to modify or explain package 、 class 、 Method 、 attribute 、 Constructors 、 Local variables and other data information .
- Just like the notes , Annotations do not affect program logic , But annotations can be compiled or run , Equivalent to supplementary information embedded in the code .
- stay JavaSE in , The purpose of annotation is simple , For example, marking outdated features , Ignore warnings, etc . stay JavaEE Annotations occupy a more important corner color , For example, to configure any aspect of the application , Instead of java EE The legacy of the old version of the code and XML Configuration etc. .
Two 、 Basic Annotation Introduce
1、 Use Annotation Add... In front of it @ Symbol , And put the Annotation Use it as a modifier . Used to decorate the program elements it supports plain
2、 Three basic Annotation:
1) @Override: Limit a method , Is overriding the parent method , This annotation can only be used for methods
2) @Deprecated: Used to represent a program element ( class , Such method ) Deprecated
3) @SuppressWarnings: Suppress compiler warnings
1)、Override Instructions
Ⅰ[email protected] Represents the method that specifies to override the parent class ( Verify from the compilation level ), If the parent class doesn't have fly Method , May be an error
Ⅱ If you don't write @Override annotation , And the parent class still has public void fly00, Still constitute rewriting Ⅲ[email protected] It can only modify methods , Cannot modify other classes , package , Properties and so on
Ⅳ see @Override The annotation source code is @Target(ElementType.METHOD), Description can only modify methods
Ⅴ.@Target It's a comment that modifies the annotation , It's called meta annotation , Remember the concept .
2)@Deprecated Annotated cases
Ⅰ. @Deprecated Decorate an element , Indicates that the element is obsolete
Ⅱ That is, it is not recommended to use , But you can still use
Ⅲ see @Deprecated Annotation class source code
Ⅳ Can modify methods , class , Field , package , Parameters wait
Ⅴ@Deprecated It can be used as a transitional version upgrade
3)@SuppressWarnings Annotated cases
- When we don't want to see these warnings , have access to SuppressWarnings Annotations to suppress warning messages
- stay {""} in , You can write what you want to suppress ( No display ) Warning message
- The types of warnings that can be specified are
.// all, Suppress all warnings
// boxing, Suppression and encapsulation / Warnings related to disassembly and assembly work //
.//cast, Suppress warnings related to forced transition operations //
//dep-ann, Suppress warnings related to obsolete comments //
//deprecation, Suppress warnings related to obsolescence //
//fallthrough, Inhibition and switch Omit... From the statement break Related warnings //
//finally, Suppressed and not returned finally Block related warnings //
//hiding, Suppress warnings related to local variables of hidden variables //
//incomplete-switch, Inhibition and switch indicative mood (enum case) Warnings about missing items in //
//javadoc, Inhibition and javadoc Related warnings
//nls, Inhibition and non nls String text related warnings //
//null, Suppress warnings related to null analysis //
//rawtypes, Inhibition and use raw Type related warnings //
//resource, Inhibition and use Closeable Warnings about resources of type // //restriction, Suppress warnings related to the use of non recommended or prohibited references //
//serial, Serializable and suppressable categories serialVersionUID Field related warnings // //static-access, Suppress warnings related to incorrect static access //
//static-method, Inhibition and may be declared static Method related warnings //
//super, Inhibition is related to the replacement method, but does not include super Call warning //
//synthetic-access, Suppress warnings related to suboptimized access to internal classes // //sync-override, Suppress warnings about missing synchronization due to permutation synchronization methods // //unchecked, Suppress warnings related to unchecked jobs //
//unqualified-field-access, Suppress warnings related to unqualified field access // //unused, Suppress warnings related to unused and disabled code
4. About SuppressWarnings The scope of action is related to the position you place
such as @SuppressWarnings Put in main Method , So the scope of restraining the warning is main
// Usually we can put specific statements , Method , class
3、 ... and 、JDK Yuan Annotation( Yuan notes , understand )
Basic introduction of meta annotation
JDK Yuan Annotation Used to embellish other things Annotation Yuan notes : Itself has little effect , When looking at the source code , You can know what he does .
Types of meta annotations
- Retention Specify the scope of the annotation , Three SOURCE,CLASS,RUNTIME
- Target Specify where annotations can be used
- Documented Specifies whether the annotation will be in javadoc reflect
- Inherited The subclass inherits the parent annotation
边栏推荐
- Pytest failed and rerun
- Zibbix installation and deployment
- Simple manual implementation of map
- Technology Management - we must focus on the big and let go of the small
- Ora-27300, ora-27301, ora-27302, ora-27303, tns-2518, tns-12549, tns-12560, tns-00519 and other alarm processing
- 软件测试面试题:假设有一个文本框要求输入10个字符的邮政编码,对于该文本框应该怎样划分等价类?
- 【2022牛客多校第二场】K-Link with Bracket Sequence I
- Software test interview questions: the steps to write test cases by drawing cause and effect diagrams are___ And transforming the cause and effect diagram into a state diagram in five steps. What are
- Will the United States prohibit all Chinese enterprises from purchasing American chips? Trump responded like this
- How to check whether there is Tsinghua source / delete Tsinghua source and keep the default source
猜你喜欢

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!

Instructions - Worthington reverse transcriptase, recombinant HIV testing program

高并发遇到死锁了,如何搞?

【2022牛客多校第二场】K-Link with Bracket Sequence I

Comprehensively design an oppe home page -- the style of the search and oper part of the page

Idea connects to MySQL database and performs SQL query operations

Characteristics and determination scheme of Worthington mushroom polyphenol oxidase

聊聊 MySQL 事务二阶段提交

Analysis of STL source code
随机推荐
OPPO造芯计划正式公布:首款芯片或为OPPO M1
Software test interview question: suppose there is a text box that requires the input of a 10 character postal code, how should the text box be divided into equivalent classes?
MobileVIT学习笔记
技术管理 - 一定要抓大放小
美国新宣布制裁的6家中国企业到底是何方神圣?
Technology Management - we must focus on the big and let go of the small
深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
异常-Exception
Microsoft store can't download apps, vs2019 can't download plug-ins solution
聊聊 MySQL 事务二阶段提交
[2022 Niuke multi School Game 2] k-link with bracket sequence I
JVM-内存模型 面试总结
Comprehensively design an oppe home page -- the style of the search and oper part of the page
单核CPU, 1G内存,也能做JVM调优吗?
Worthington plasma amine oxidase (PAO) instructions
Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design
IDEA连接MySQL数据库并执行SQL查询操作
Oppo core making plan officially announced: the first chip or oppo M1
DAY_ 4. Operation -- judge whether there is a certain data in the array -- realize array mapping (enlarge by 10 times) -- insert the array in sequence (modify bugs) -- realize array de duplication
软件测试面试题:假设有一个文本框要求输入10个字符的邮政编码,对于该文本框应该怎样划分等价类?