as a parameter, and the value of the parameter is the value of the variable name.3. Variable name (variable name corresponding value) {Static code block}//except 2, the enumeration class also defines an abstract method, and the code block after the variable name corresponds to an anonymous inner class that implements the abstract method. As follows: Variable type variable name =new
Enumeration definition:An enum is a data type in a computer programming language. Enumeration types: In real-world problems, some variables are scoped to a limited range . For example, there are only seven days in one weeks, 12 months in a year, six courses per week in a class, and so on. If these quantities are described as integers, the character type or other
() ; } } Public Connection getconnection () { return Connection; }}The Run method of the custom line thread is modified to: public void Run () { System.out.println (singleton.connectionfactory. Getconnection (). Hashcode ()); }
103201006910320100691032010069
5. Perfect using Enum enumeration to implement Singleton modeThe preceding section exposes enumerated classes, violat
The enumeration classes are new after Java 5, can be used to encapsulate constants, and can also provide some methods for the use of constants.Defines the syntax for an enumeration class: Public enum enumname{ //must be placed on the first row of member 1 (A, B ... ), member 2 (A, B ... ), member 3 (A, b ...). )........; //a,b can not P
Java Enum usage example and javaenum usage example
Public enum MyEnum {Monday, Tuesday, Wednesday, Thursady, Friday, Saturday, Sunday; public static void main (String [] args) {// Enum object MyEnum mye; mye = MyEnum. sunday; MyEn
I. Grammar1. Enum is all called enumeration, Chinese is commonly known as enumeration class, learn C + + and other languages of the people, should be to it knows. In the Java language Specification, however, it was introduced in the JDK version 5 and stored in the Java.lang package. In the Java version of the
, and fields to the enumeration type. Of course, the constructor is called only when constructing enumeration constants. A facet is an example:public enum Size{small ("S"), MEDIUM ("M"), LARGE ("L"), Extra_large ("XL");p rivate String abbreviation;private Size ( String abbreviation) {this.abbreviation=abbreviation;} Public String getabbreviation () {return abbreviation;}}All enum types are subclasses of the
In some cases, the object of a class is limited and fixed, such as the seasonal class, which has only 4 objects. This example of a finite and fixed class, in Java, is called an enumeration class.In the early days, the enumeration classes might be represented directly using s
With the effective Java this book to see more and more, found in peacetime use of Java made a lot of obvious mistakes, they usually know a little but the lack of use is also more and more.As for enumeration classes, the suggestion in the book is to replace the traditional static constants with enumeration classes, usually, if a class of data is represented by a d
Long time no contact enumeration class, almost forget, today take a moment to summarize it. To be honest, enumerating classes can really make a lot of sense for us.Description: Enum class It contracts a scope, it can be understood that only a fixed number of objects can be generated to call the outside world, so the constructor method in the enumeration
Enum class Enum is introduced by JDK1.5, and is preceded by a public static final int Enum_value instead of an enumeration class. Enum class Enum is a special
In Java, serialization of an enum type differs from the serialization of other object types, to see what is different today. Let's look at how the enum we defined in Java is going to look like after being compiled.
Java code:
Java
encapsulation and code readability. For example, use constant types to set and compare property values:
Setting property values
Sex Sex = Sex.male;
Comparison
if (sex = = Sex.male) {
// ... ...
}
If the sex is defined with an enum, the value of the enum field is actually compared, and if the sex is defined using the Enum
PackageCn.edu.shu.web.util;/** * * @author Tkpad [ Email protected] * @version V1.0.0 * */ Public enumFileType {WORD (1), TXT (2), ANNO (3);//Note that the sentence must be placed in the first line Private intType//define the variable used in the database, or the value of the enumeration variable //Use a constructor to assign a value to a defined enumeration variable Private FileType(intType) { This. type = type; }//By overwrite the ToString method, you can get the value of the enume
intSeason_winter=4; the the Enumerate class instances: the Public enumseasonenum{ the //4 enumerated instances in the first row - Spring,summer,fall,winter; in } the the //Test Class About Public classenumtest{ the Public voidjudge (Seasonenum s) { the //the expression in the switch statement can be an enumeration class the Switch(s)
1, the enumeration class is also a class, you can have attributes, and so on:public enum Seasonenum{spring,summer,fall,winter;}This is not the other attribute.Package Com.creditharmony.adapter.testcase.test;public class Enumtest {public void judge (Seasonenum s) {switch (s) {case SPRING:System.out.println ("Spring"); B
This error occurs: A class, interface, or enum hint is required, generally in two cases:1. The code is incorrectly written;2. There is a problem with the Encoder selection encoding format. ( mainly in non-Notepad to write code files, there is an encoding format conversion problem.) )such as the error message, we can see that the encoding format caused by the error, the solution is to modify the encoding for
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.