The enumeration class is defined as follows:Package com.qunar.enumtest; Public enumStatus {scuuess ("1","Success"), FAILED ("2","Failed");PrivateStringvalue;PrivateString desc; PublicStringGetValue() {return value; } Public void SetValue(Stringvalue) { This.value=value; } PublicStringGetDesc() {returnDesc } Public void Setdesc(String desc) { This. desc = desc; }Private Status(Stringvalue, String desc) { This.value=value; This. desc = desc; }}Use the f
Note Item:1. Using enumerations in switch can make your code more readable. 2. If you want to customize a method, you must add a semicolon at the end of the enum instance sequence.And Java requires that an enum instance be defined first.3. All enumerations inherit from the Java.lang.Enum class, and because
JDK1.5 introduces a new type--enumeration. In Java, although it is a "small" function, but to my development has brought "big" convenience.Usage One: constant.Before JDK1.5, we defined constants: public static fianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants. Public enum Color { RED, GREEN, BLANK, YELLOW}Si
Directly on the code:1 Public enumBoss {2 3 INSTANCE;4 5 PrivateString name;6 7 Public voiddosomething () {8SYSTEM.OUT.PRINTLN (name + "is doing something now ...");9 }Ten One PublicString GetName () { A returnname; - } - the Public voidsetName (String name) { - This. Name =name; - } - +}Note: The enum in Java is a thoroughly-bottomed
Java programming: attaches methods to enumeration constants in enumeration enum
Package enumeration; // append the method to the enumerated constant public class UnitsConverter {private static double numberToConvert = 0; public static void main (String [] args) {// TODO Auto-generated method stub if (args. length = 0) {System. out. println ("Usage:
() Public intToInt () {return This. Value; } PublicString Totext () {return This. Text; } //you can define a static method to get text based on value (provides methods for an enumeration class)//Testenum.gettext (1) Public StaticString GetText (intvalue) { for(Testenum item:TestEnum.values ()) {if(Value = =item.value) {returnItem.text; } } return NULL; } //you can override the
The enumeration class is defined as follows:Package com.qunar.enumtest; Public enumStatus {scuuess ("1","Success"), FAILED ("2","Failed");PrivateStringvalue;PrivateString desc; PublicStringGetValue() {return value; } Public void SetValue(Stringvalue) { This.value=value; } PublicStringGetDesc() {returnDesc } Public void Setdesc(String desc) { This. desc = desc; }Private Status(Stringvalue, String desc) { This.value=value; This. desc = desc; }}Use the f
//print a specific value; Public classDemo1 { Public voidTest () {print (GRADE.A); } Public voidPrint (Grade Grade) {//print a specific value;String value=Grade.localvalue (); System.out.println (value); } /** Method One static class Grade{public Grade () {} private static final Grade A =new Grade (); private static final Grade b=new Grade (); private static final Grade c=new Grade (); private static final Grade d=ne
anonymous classes without the class keyword?1) to have the new operator, similar to the normal class instantiation of an expression.2) to implement an interface or inherit a class, the example above is to implement the HelloWorld interface.3) class body (body), where you ca
a c ++ nested class is whether it points to external class references. Of course, there are some differences from the design details and some other perspectives. In addition, no non-static internal class can have static data, static methods, or another static internal class (internal
Java Tour (ix)--object class, Equals,tostring,getclass, inner class access rule, static inner class, Inner class principle, anonymous inner class
Every day by some trivial harassment, learning to continue
I. Object
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.