Java Fundamentals 5

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.