Today fix a company's very early Android app feature, the code logic is completely confused, and then found that the data returned is completely incorrect. And then fix it for a full two days. And then I tidied it up again, and refactoring is not even up.
The enumeration is then used.
What is an enumeration? I didn't understand it at the time, and I saw the company's project using enumerations as a control of the project, such as changing the already written app and making a custom version for some handset makers. You may want to get rid of ads, and jump to the store URL are different, especially in the domestic basic no Google Play. We are trying to avoid future changes. An enumeration is written to control it.
Public enum Market {Default,huawei () {@Overridepublic String Getmarketurl () {return "http://play.huawei.com";//huawei Market Url}},zte () {@Overridepublic Boolean isshouldad () {return false;} @Overridepublic String Getmarketurl () {return "http://play.zte.com";//zte market Url}},onetouch () {@Overridepublic String Getmarketurl () {return "http://play.huawei.com";}}; public Boolean Isshouldad () {return true;} Public String Getmarketurl () {return "Http:\\googleplay ...";//google play URL}}
The example above is about some of the basic uses of Java enumerations in Android.
To understand the principles of Java enumeration, I wrote a sample of a traffic light that was not used frequently. Here is the code with the enumeration:
public enum TrafficLight {red {@Overridepublic trafficlight Nextlamp () {return Green;}},green () {@Overridepublic TrafficLight Nextlamp () {return Yellow;}},yellow (3) {@Overridepublic TrafficLight nextlamp () {return red;}}; private int time;private trafficlight (int time) {this.time = time;}; Public abstract TrafficLight Nextlamp ();p ublic int getTime () {return this.time;}}
then the code for the normal class simulation enum:
Public abstract class TrafficLight {public static final trafficlight red = new TrafficLight ($) {@Overridepublic Traff Iclight Nextlamp () {return green;}; public static final TrafficLight green = new TrafficLight (+) {@Overridepublic TrafficLight nextlamp () {return yellow ;}}; public static final TrafficLight yellow = new TrafficLight (3) {@Overridepublic TrafficLight nextlamp () {return red;}} ;p rivate int time;private trafficlight (int time) {this.time = time;}; Public abstract TrafficLight Nextlamp ();p ublic int getTime () {return this.time;}}
By comparing the two, you will find that the enumeration is actually a common Java class. The constructor is only private, and then a few static final instance variables are provided.
Of course, Enum also provides some other methods.
For example: TrafficLight.green.name () is still very useful.
These are some of the basic applications of enum. Then is how I apply the types of enumerations in the project today. Because our app has three different URL for requesting data.
In fact we only have one data source, assuming that find is not. It will pass through the other two to read the HTML of the other site. The data is then parsed and obtained by means of a regular table-matching match. Each data source needs to set HttpClient, HttpGet, HttpResponse, and so on, and then uses the enumeration.
I put a little bit of the main here. And then it turns out that it's almost the same.
Import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;public enum Requestedprovider {myself () {@Overridepublic String getUrl (String KeyWord) {return "http://..." + KeyWord + "...";} ...},google () {@Overridepublic string GetUrl (String keyWord) {return "Http://google ..." + KeyWord + "...";} ...},amazon () {@Overridepublic string GetUrl (String keyWord) {return "Http://amazon ..." + KeyWord + "...";} ...}; Public abstract String GetUrl (String keyWord);p ublic HttpClient pickhttpclient () {return new defaulthttpclient ();} Public httpget pickhttpget (String URL) {return new HttpGet (URL);} Public HttpResponse Pickhttpresponse (HttpClient client, HttpGet get) {HttpResponse res = null;try {res = Client.execute (ge t);} catch (Clientprotocolexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return res;} ...}
That's what I'm going to write about today. ye!
Java enumeration in Android project app