Switch in Java uses string as the condition

Source: Internet
Author: User

In the process of developing Java programs, we encountered a conditional inference preferred is switch, but the switch feature in Java does not support strings as a condition. What should we do then? --use enumerations.

First, enumeration simple understanding

1, enum is a data type.

Special: Refers to the value of the variable in the enumeration is listed in one by one, the value of the variable is limited to the value enumerated in the range.
Special to generalization: ENMU is the same data type as "string,int" that we need to define according to our business needs. At the same time, an enum is a class that can define a method in an enum and invoke it in the same way as the calling class when it is used.

2, the premise of definition as enumeration

Used to declare a set of named constants, when a variable has several possible values, and the values are fixed, and generally do not change, it can be defined as an enumeration type. For example: Seven days a week:
Public Enum Day
{
SUNDAY, MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY,
}

3. Base class for enumerations

The System.Enum type is the abstract base class for all enumerated types, and members inherited from System.Enum are available in any enumeration type.

In this base class, inherits the system's three interfaces IComparable, IFormattable, IConvertible. There are very many methods that are used frequently. For example: Compaareto,equals,format, so when using the enumeration class, do not simply be based on business need to define, to learn to use the inheritance system already exist in the "base class", you can have a giant to write a good way to save their own trouble writing, more importantly, "giant" The flexibility to write is good.


Second, examples

The following is a small "perspective" in a DRP. Different tables are manipulated according to different strings, using the factory schema and using enumerations in Factorycontext to complete the switch inference.

/*<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > * Define data Dictionary enum class * */public enum datadictenum {a,b,c,d;} /* * Use switch to infer that the number of parameters is enumerated as enum type * Different factory objects are instantiated according to A,B,C,D in the enumeration * */public idatadict createfactory (datadictenum type) {idatadict di Ctfactory=null;switch (type) {//Instancing Distributor factory class case a:dictfactory= clientlevelfactory.getinstance (); Case b:dictfactory= regionlevelfactory.getinstance (); return dictfactory;} </span><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >//Call//create Enum type in "A" Datadictenum type =datadictenum.a;//instantiate reseller, call switch Inference method Datadictmanager CLM = Datadiccontext.getinstance (). Createfactory (Type). CreateType ();</span>

As simple as that, using enumerations to conquer Java in the switch cannot infer that the string is imperfect.

PS: The premise of enumeration is that these strings do not change frequently and it is not recommended to use enumerations if they are changed frequently.






Switch in Java uses string as the condition

Related Article

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.