, characteristics, scenarios, and pros and cons of enumeration types.2 DefinitionsAn enumerated type (enum type) refers to a fixed set of constants that make up a valid type. In Java, the keyword enum defines an enumeration type. The following is the definition of the Java enumeration type.Java code
Public
Test {public static void main (string[] args) {for (Color color:Color.values ()) {String label = Color.getlab El (); SYSTEM.OUT.PRINTLN (label);}}}
• enumerate nouns . enumeration of Image : Gets a specified enumeration object, as in the following example:
static t[] values () : Gets all enumerated objects in the custom enumeration, as in the following example
• Enumeration types can be used in switch, a
Java Development often uses a limited number of constants, which can be defined within the bean as an enum type, so in the maintenance of more convenient to control, so here is a simple record of the definition and usage of enum, convenient for future direct reference use, lazy people plan, hehe ... OK, just look at the code ...
1,
enum+ polymorphic, I did not say wrong, but Enum is not can be inherited, also can not inherit from others, just can realize interface, what talk about polymorphism?But let's look at the phenomenon first: Java code
public enum Fruit {APPLE, PEAR, PEACH, ORANGE; }
The above is a simple
I. Appearance of the background:Before JDK1.5, we defined constants as this: public static final String red = "red";With the addition of enumeration types in JDK1.5, we are able to group related constants into an enumeration class:public enum Color {RED, GREEN, BLUE;}This is possible when used: Color C = color.redTwo. Enumeration features:1. Enum Enum is a specia
The Java enum is singleton class (only one instance). Java uses compilers and JVMs to prevent an enum from generating more than one class: You cannot use the new, Clone (), De-serialization, and Reflection APIs to produce an enum'
Features of Enum in Java 5:1. Enum is a class;2. Enum is final and cannot be inherited, and the value cannot be overwritten;3. You can use "=" and "equals ()" to compare enum;Enum befor
One. struct typestruct types, common body types, enumeration types, class types, and so on are collectively referred to as custom types (User-defined-type,udt).Structs are equivalent to records in other high-level languages (record), for example:struct student{int num;Char name[20];char sex;int AgelFloat score;Char addr[30];};The general form of a struct declaration:struct struct type name {member List};The struct type name is used as a flag for the s
The enumeration type is a new feature of the JDK5.0. Sun introduced a new keyword enum to define an enumeration class. The following is a definition of a typical enumeration type:
Java code public enum color{Red,blue,black,yellow,green}
Obviously, the enum is very much like
enumeration type to use the enum keyword implies that the type created is a subclass of the Java.lang.Enum class (Java.lang.Enum is an abstract class). The enumeration type conforms to the generic pattern Class enumpublic enum Enumtest {MON, TUE, WED, THU, FRI, SAT, SUN;}Th
Eclipse can intelligently convert Utf-8+bom files to normal UTF-8 files, and Android Studio does not yet have this feature, so "illegal characters: ' \ufeff '" will appear when compiling Utf-8+bom encoded files using Android Studio. and other errors. Such as:If you encounter this problem, you can manually convert the Utf-8+bom encoded file to a normal UTF-8 file.Small series here use EditPlus to convert, open with EditPlus. java files in order: Docume
of enum classes. If you explicitly specify additional access controls for the constructor of the enumeration class, an error is compiled. Other than that. Note All instances of an enumeration class must be explicitly listed in its first row, otherwise it cannot produce an instance.For example, here is a classic demo s
I. Appearance of the background:Before JDK1.5, we defined constants like this: public static final String red = "red";The enumeration types are added to JDK1.5, and we can group related constants into an enumeration class:public enum Color {RED, GREEN, BLUE;}This can be used: Color C = color.redTwo. Enumeration features:1. Enum Enum is a special
JAVA Inner class
One, what is the internal class?
The definition of a class is placed inside another class, and this class is called an inner class
What are the characteristics of th
Java enum enumeration usage
1. Background:
Before JDK1.5, we define constants as follows: public static final String RED = "RED ";
When the enumeration type is added to JDK, We can group related constants into one enumeration class:
public enum Color {RED, GREEN, BLUE;}
This can be used as follows: Color c = Color. RE
class reference to the subclass instance of new, so what happens to the interface?Example 3: Anonymous class implementation interfaceFirst define an interface public interface Human {public void eat ();p ublic void Drink ();}public class Unnamedclass {public static void main (string[] args) { As you can
This example describes the use of an enum in Java. Share to everyone for your reference. The specific analysis is as follows:
1. Basic usage
Copy Code code as follows:
Enum Day {
SUNDAY, MONDAY, Tuesday, Wendsday, Thursday, FRIDAY, SATURDAY;
}
Enumerations are constants, so you should use uppercas
Static void Main (string[] args) { = enumsingleton.getinstance (); = enumsingleton.getinstance (); // output Result: Obj1==obj2?true System.out.println ("Obj1==obj2" + (obj1==obj2));}To extend the application, observe the following example Public class test{ // initialize something static{ }}This is a common way of initializing static resources within a
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.