当前位置:网站首页>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
边栏推荐
- 美国新宣布制裁的6家中国企业到底是何方神圣?
- IDEA常用快捷键及设置方法
- How to check whether there is Tsinghua source / delete Tsinghua source and keep the default source
- @Component可以和@Bean 用在同一个类上吗?
- 软件测试面试题:软件验收测试包括正式验收测试、alpha测试、beta测试三种测试?
- CBAM learning notes
- 2019q4 memory manufacturers' revenue ranking: Samsung fell 5%, only SK Hynix and micron maintained growth
- Can China make a breakthrough in the future development of the meta universe and occupy the highland?
- Software testing interview question: what is regression testing?
- 递归/回溯刷题(上)
猜你喜欢

Mobilevit learning notes

MySQL执行过程及执行顺序

Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?

微软商店无法下载应用,VS2019无法下载插件问题解决方案

LInkedList底层源码

ECCV 2022 | China University of science and Technology & jd.com proposed: data efficient transformer target detector

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

Puzzle (021) eliminate problems

哈希表的查找与插入及删除

30 minutes to thoroughly understand the synchronized lock upgrade process
随机推荐
微软商店无法下载应用,VS2019无法下载插件问题解决方案
Mobilevit learning notes
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?
软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
Comprehensively design an oppe homepage -- Design of selected accessories on the page
Ziguang zhanrui: dozens of 5g terminals based on chunteng 510 will be commercially available in 2020
ZABBIX monitoring service (III) configuration management graphics and windows
@Autowired注解与@Resource注解的区别
Software test interview question: please say who is the best person to complete these tests, and what is the test?
深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
Simple manual implementation of map
递归/回溯刷题(上)
University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
ADB ~ hide or disable the status bar and virtual keys
zibbix安装部署
Puzzle (021) eliminate problems
屏蔽自动更新描述文件(屏蔽描述文件)
软件测试面试题:在windows下保存一个文本文件时会弹出保存对话框,如果为文件名建立测试用例,等价类应该怎样划分?
单核CPU, 1G内存,也能做JVM调优吗?
Acwing3715. 最少交换次数(冒泡排序法的模拟思路)