1. Enumeration
Enum creation: Enum enum name {}
Role of enumeration: limit the range of values
Attributes of the enumeration:
1, enumeration is a special class. Abstract class
2. Each enumerated value declared in an enumeration class represents an instance object of an enumeration class. A static object instantiated through an anonymous inner class.
3. As with normal classes in Java, when declaring an enumeration class, you can also declare a property, method, constructor, but the constructor of the enumeration class must be private.
4. If there is only an enumeration value in the enumeration, you can not end it. If you have other members, you must end the enumeration values.
5. There can be abstract methods in enumerations.
public class Demo6 {
public static void Main (string[] args) {
Abstest t=abstest.t;
}
}
Abstract class abstest{
public int i; Declaring a property
Public abstest (int i) {
Construction method This.i=i;
}
The following code creates an Abstest object, but it is an abstract class that cannot be instantiated and instantiated using an anonymous inner class.
public static final Abstest t=new Abstest (1) {
public void Show () {
System.out.println ("OK");
}
};
Abstract methods
public abstract void Show ();
}
The corresponding enumeration
Enum enumdemo2{
A (1) {
public void print () {
}
},b (2) {
public void print () {
}
},c (3) {
public void print () {
}
} ; Enumeration values
public int i; Member properties
Enumerations can have a constructor method.
Private EnumDemo2 (int i) {
This.i=i;
}
The permission modifier for a constructed method in an enumeration can only be private
Member Methods
public void Show () {}
Abstract methods
public abstract void print ();
}
Java Fundamentals 5