1 Packagetimers;2 3 ImportJava.util.Timer;4 ImportJava.util.TimerTask;5 6 /*7 * Mainly want to use the object of this class in another class, how to use it? How to pass the same instance object8 */9 Public classTimerdemo {Ten Public Static voidMain (string[] args) { OneTimer T =NewTimer (); AT.schedule (NewMyTask (t), 3000); - } - } the - classMyTaskextendsTimerTask { //In order to use the T o
on the values in the enumeration, and the encapsulation is better.
InstanceLet's first define a simple enumeration (this is just an example of a simple definition of 3 variables):
public enum Sqltypeenum {
INSERT,
UPDATE,
DELETE,
SELECT
}
After parsing the SQL, we get a token, how do we get this token enumeration?
This gets:
String token = "select";
Sqltypeenum sqltypeenum
Enumeration as a general language concept, until the birth of Java5 has to say a bit strange, so far many programmers still prefer to use static final form to name constants without using, in general, Java programmers in this way to implement enumerations:Class enumbyclass{public static final int red=0; public static final int green=1; public static final int blue=2;}An enumeration implemented in this way is also called an int enumeration pat
;
PrivateSex4 (Stringsex ){
This. Sex = sex;
}
PublicString toString (){
ReturnSex;
}
}
In this way, you can use the Sex4 type to ensure that the gender can be a BOY or GIRL. For example, the following function definition:
PublicvoidOutSex (Sex4sex ){
System.Out. Println (sex );
}
But now the problem comes again. We only need to use two constants, but we need to write so many lines of code (when defining Sex4). Is there a simple way? The answer is, of
enumeration as a bit field. For more information, see flagsattribute.Implemented Interface
This class inherits from valuetype and implements icomparable, iformattable, and iconvertible interfaces. Use the convert class for conversion, instead of using the explicit interface member of this class iconvertible.Guidelines for flagsattribute and
SAT = "Sat"; String SUN = "Sun ";}
With enumeration, we can use the following method:
public enum WeekEnum { MON, TUE, WED, THU, FRI, SAT, SUN;}
Explanation: To create an enumeration type, use the enum keyword, which implies that all created types are subclasses of the java. lang. Enum
Override the ToString () of the class to simplify obtaining the DescriptionAttribute value of enum,Override the ToString () of the class to simplify obtaining the DescriptionAttribute value of enum.
Directory
I. Common enum versions
Ii. Evolution:
Override the ToString () of the class to simplify obtaining the DescriptionAttribute value of enum,Override the ToString () of the class to simplify obtaining the DescriptionAttribute value of enum.
Directory
I. Common enum types
Ii. Evolution:
1. About Java Enum:
Anyone who has studied C/C ++ or other languages should have a slight knowledge of the Enum type. Enum is generally used to represent a group of constants of the same type. Such as gender, date, month, and color. The benefit of using constants for these attributes is obvious. Not only can the single
Sex4 ( public staticfinal Sex4 Boy =New Sex4 ("GIRL"); Private Stringsex; Private Sex4 (Stringsex) { this. Sex = Sex ; } public String toString () { returnsex; }}In this way, you can use the SEX4 type to ensure that the gender can be a boy or a girl if you need to use sex elsewhere. For example, a function similar to the following definition: Public void outsex (Sex4sex) { System. out . println (sex);}But now that's the problem,
Recently in the Hadoop#yarn part of the source code, read the state machine that part of the time, feel the use of ENMU is too flexible, in the concurrent programming network to translate an article, just met an article like this, quickly translated down, rose posture.Original link: http://www.javacodegeeks.com/2011/07/java-secret-using-enum-to-build-state.htmlPeter Lawrey Translator: Chen ZhenyangReviewEnm
Gain an in-depth understanding of enum in Java and Javaenum
We can use either the public static final String constant or the enum String constant to represent a limited number of things in a program. In general, the former is simple, but it cannot provide more information. In Java,
The introduction of enum from jdk1.5 is actually a special class, which is easy to write. JVM automatically encapsulates a lot of things for you.
Package scjp;
The compiled Enum (coffeesize above) also generates a coffeesize. Class
Decompilation through class assemblyJava
Enum is called enumeration. It is a new feature introduced in JDK 1.5 and is stored in the Java. lang package.
The following are some of my experiences and conclusions in using Enum, including:
1. Original interface definition constant
2. syntax (Definition)
3. Common Operations such as traversal and switch
4. Introduction to common methods for
Enum enumeration class ① Differences between enumeration classes and common classes:The enumeration class defined by Enum inherits the java. Lang. Enum class by default.The constructor
actual call, and the compiler will make an error.For example, the form's calling function is as follows:private void Button1_Click (object sender, EventArgs e) { Testenum testenum = new Testenum ((thefirstenum) random. Next (3), (Thesecondenum) random. Next (3)); MessageBox.Show (Testenum.value); }The constructor was removed from the first Enum section and resumed normal
For something that you want to represent a finite kind of thing in a program, generally we can do it in two ways: public static final String constant, and second, using Enum to represent. In general, the former is simple, but not very good to provide more information, and in Java, the enum is very strong, but also more professional. 1. most C-style
GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int GetIndex () {return
index;
}
public void Setindex (int index) {
this.index = index;
}
}
usage Four: Methods of overriding enumerations
An example of a ToString () method overlay is given below.
The public enum Color {red
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.