Enumerate the basic knowledge of Java and related interview questions

: A definition of the enumeration.
, Is a collection from a new enumeration data type provided Java5 started, a special class of a plurality of stationary objects constant.
Definition Format:
[Modifier] Enumeration enum class name
{
constants a, B constants, the constants C; S
}
our custom enumerated type, the compiler (at the bottom) is directly inherited from class java.lang.Enum, the enum enumeration of all parent

Two equivalence relation enumeration and the general classes:
Here Insert Picture Description
three enumerated classes after the decompile:
Here Insert Picture Description
four enumeration class features:
① enumeration direct parent java.lang.Enum, but can not display inherited Enum.
② enumeration is equivalent to a class, you can define the constructor, member variables, methods, and general abstract methods.
③ default private manufacturing method, i.e., write access is not Private ((prosthesis constructor, not the underlying non-argument constructor).
④ each instance are represented by a global constant, enumerate the object class is fixed, one example limited number, you can not use the new keyword.
⑤ enumerate instances must be in the very beginning of the enumeration body, there must be a semicolon after the enumeration list of instances with other members separated.
there braces after ⑥ enumerate instances when the examples are anonymous inner class object enumeration class.

Five Use enumerated:
1): enumeration are global public static constants can be directly used to enumerate the class name calling.
Weekday Day = Weekday.SATURDAY;
2): Because java.lang.Enum class is all gold For the parent class, so all the enumeration object enum class methods can be invoked.
String name = enumerable object .name (); // returns the enum constant object name
int ordinal = enumerable object .ordinal () ; // returns an enumeration object number, starting from 0.
String = enumeration object .toString STR (): returns enum constant name of the object
3): static method enumeration class compiler-generated (compiled code from trans ) is:
enum type [] values ();
Weekday [] WS = Weekday.values () returns all ;: enumerated type constant current, using an array encapsulated.
enumerated type valueOf (String name);
Weekday Day = Weekday.valueOf ( "MONDAY"); // converts a designated name string class with the same name as the current enumeration constants.
4): the enumeration began java5, switch also supports operation enumeration type.
Switch supports only int type, because ordinal enumeration support enumerable object underlying the use of, and is still the type ordinal i nt type (as shown below).
Here Insert Picture Description
5): Using the enumerated classes do Singleton:
(singleton achieve the best model is the use of enumeration enumeration mode features allow JVM to help us ensure the security thread and a single instance. problem. In addition, the wording is also particularly simple.)

public enum Singleton {
    INSTANCE;
    public void doSomething() {
        System.out.println("doSomething");
    }
}

Call the method:

public class Main {
    public static void main(String[] args) {
        Singleton.INSTANCE.doSomething();
    }
}

It can directly call through Singleton.INSTANCE.doSomething () way. Convenient, simple and safe.

Published 99 original articles · won praise 2 · Views 2627

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105107101