Enumeration, custom annotation

Copyright: https://blog.csdn.net/qq_39263750/article/details/89645157

enumerate:

Before:
Here Insert Picture Description
the result is normal returns 1. But if introduced to a range of 0-6 is no longer the number,
Here Insert Picture Description
the compiler what code runs fine, but not what we need him to pass a value that is not restricted. All this time the incoming parameter does not make sense. We define the exe when explicitly accept that int day, but because it is an int, so I can not restrict incoming value, there has been unsafe types of problems.
Use constants to represent the common values on the page:
benefits: a modified everywhere modification, after the success of reduced maintenance, constant in the code tell at a glance, during compilation has been able to confirm the memory space

Question:
1. Type of insecurity: documented column we need to pass a parameter to the date method, the method is int, when you pass arguments do not necessarily have to pass a constant value.
2. No Namespace: Constant naming of high demand, once irregularities. The latter can not understand the significance of the parameters
constant integer used in the case of columns: 3. consistency is poor. In the compiler to complete the memory operation data. In many places we have cited the current value. When you change the list or increase the value listed, all local references need to be recompiled,

This time it was introduced to enumerate :

Enumerated type

What is an enumeration

A method for the enumeration is set to a value contained In a similartype of datain. Enumerated type named will become the enumerationUnparalleledType Description variable to compare with the use of constants, enumerated types can declare the range greater than constant.

For example:
a person's gender: male | female one week only seven days in a month up to 31 days a year for up to 12 months

For objects: 4 seasons a year (Object)

When a variable has a value of several fixed time, or a limited number of objects. You can use an enumeration to represent.

Enumerate the basic use

-Class statement declaring class interfaces -interface statement enumeration -enum

Syntax: Public enum enum {name}

Enumeration members:

Fixed value
Here Insert Picture Description
at this time, if you need to get Monday, you can order it directly
Here Insert Picture Description
Here Insert Picture Description

This time, only to find later Monday to Sunday, another day could not be found.

Enumeration of the benefits brought

Chestnuts:
Here Insert Picture Description
by enumerating Optimization:
Here Insert Picture Description
readability greatly improved

Enumeration Features:

1. Any two enumeration values can not have the same name.
Use commas to separate multiple enumeration values 2.
3. If you do not declare enumerated data type display, the system will automatically set the data type int type

The benefits of using enumeration:
1. Use the enumeration class enhances the readability of the code.
2. convey the scope of some constraint values

Advanced: enumerated data types into objects

The object here is not to take over directly to userbean with these objects to be herearrogant, The constructor must be definedprivate(This construction method can only produce one, can not be said to produce a number of enumeration constructor)
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
this spring when it came out.
Here Insert Picture Description
"Spring" is not found in printed value previously set
Here Insert Picture Description
Here Insert Picture Description
is now just fine.

Summary : In the enumerable object declared objects:
1. The enumeration values must be the first line
2. Declare enumeration object when the constructor must be private. (Purpose: so you can not enumerate this object elsewhere in the statement by designing immutable classes inside his property should be set to the final, which is more secure, and the code is more concise.)

Enumeration method

Each enum class in Java inherits from class java.lang.Enum, when the definition of an enumerated type, each enumeration member can be seen as an instance of Enum class.
Each instance has enumerated the following methods:
Here Insert Picture Description

代码演示:
values()方法:以数组的形式返回枚举的所有成员,然后通过foreach方法循环输出。
Here Insert Picture Description
valurOf():将普通的字符串转换成枚举实例
Here Insert Picture Description
Here Insert Picture Description
Oridinal:获取枚举成员索引位置
Here Insert Picture Description

枚举和接口

Here Insert Picture Description

同样的,也可以在接口里面使用枚举来组织你的内容。
Here Insert Picture Description
用法:
Here Insert Picture Description

枚举同样支持继承抽象类。

Java是单继承模式,只能继承一个类。每个枚举已经继承了java.lang.Enum。但是不妨碍他实现接口
1.跟普通类一样实现接口,重写抽象方法
2.在接口中使用枚举来组织内容
3.可以在抽象类中,也使用枚举来组织内容EnumMap在枚举类中提供了一个对象EnumMap对象,和hashmap作用是一样,用于存储键值对。数据结构不一样。EnumMap存放的Enum数据类型。底层使用数组来组织内容。在使用这两个对象的时候EnumMap效率更高。

枚举提供的两个特殊的对象:EnumMap、EnumSet

EnumMap是专门为枚举定义map
认识即可,知道是装枚举类型的就行。
如何定义?
Here Insert Picture Description

EnumSet:是枚举类型的高性能set,要求放入其中的枚举常量必须是同一枚举类型。效率非常高、比hashset效率高。枚举底层元素比较简单,hashset底层比较复杂。


自定义注解:

Servlet in the notes:
the @WebServlet used to configure the servlet, servlet will be loaded when tomcat starts.
Here Insert Picture Description
@WebFilter This annotation is used to configure filters
Here Insert Picture Description
What is a comment?
What can solve the problem?
How to learn?
How to optimize?

The basic concept of annotation
comment is very important in the learning process behind the 80 percent are to be developed based on the comments. Spring, hibernate, springmvc frame
annotation is a trend, annotations are only put forward after jdk1.5

Jdk1.5 after the proposed three notes:

@SuppressWarnings: Compression warning
@Deprecated: method definition obsolete
@Override: Rewrite

Annotation: annotation corresponding to a marker, add comments in the program equivalent to the program tagged, the javac, or other types of development tools may be acquired according to the above mechanism based annotation reflected, in accordance with the completion of the corresponding operation function annotation. Tags can be placed classes, attribute parameters and local variables, process, process.

Focus on learning custom annotation:

Notes development process.

Custom annotation
about custom annotation process

Guess you like

Origin blog.csdn.net/qq_39263750/article/details/89645157