当前位置:网站首页>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
边栏推荐
- Yyds dry inventory learn how to write function overloads in typescript
- Basic usage of two-dimensional array
- 2021-11-05理解main方法语法、代码块及final关键字
- Software testing interview question: what is regression testing?
- 面向3nm及以下工艺,ASML新一代EUV光刻机曝光
- 声扬科技正式上线闻声远程声纹健康回访服务系统!
- Unit-- read Excel
- Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
- 腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?
- 美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
猜你喜欢

Instructions - Worthington reverse transcriptase, recombinant HIV testing program

看起来是线程池的BUG,但是我认为是源码设计不合理。

如何实现一个好的知识管理系统?

LinkedList underlying source code

Microsoft store can't download apps, vs2019 can't download plug-ins solution

@Component可以和@Bean 用在同一个类上吗?

Dual process theory and triple mental model

MySQL执行过程及执行顺序

腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?

Can China make a breakthrough in the future development of the meta universe and occupy the highland?
随机推荐
高并发遇到死锁了,如何搞?
二维数组的基本用法
Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
QModbus库使用,并作为ROS节点发布话题及程序CMakelist编写
Dual process theory and triple mental model
[day_4-review, basic concepts of objects and arrays - 1]
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
V2.x synchronization is abnormal. There are a lot of posts that cannot be synchronized in the cloud, and the synchronization is blocked and slow
哈希表的查找与插入及删除
Microsoft store can't download apps, vs2019 can't download plug-ins solution
STL源码剖析
异常-Exception
LinkedList underlying source code
CocoaPods 重装
聊聊 MySQL 事务二阶段提交
crsctl中,显示的HOME的作用
单核CPU, 1G内存,也能做JVM调优吗?
After sorting (bubble sorting), learn to continuously update other sorting methods
Comprehensively design an oppe home page -- the style of the search and oper part of the page
Daily Mathematics Series 60: February 29