java枚举在android项目应用
来源:互联网 发布:电视围棋直播软件 编辑:程序博客网 时间:2024/06/10 18:43
今天修复一个公司很早以前的android应用功能,里面的代码逻辑已经完全错乱,然后发现返回的数据完全不对了。然后修复了整整两天。然后我重新整理了一遍,重构就算不上了。然后就用上了枚举。
什么是枚举?我以前也不懂,当时我看见公司的项目中使用了枚举当做项目一个控制,比如修改已经写好的app然后为一些手机厂商做定制版。可能要去掉广告,还有跳转到商店url都不同,特别是国内基本都没有google play。我们为了避免以后的修改,就会写个枚举来控制它。
- public enum Market {
- Default,Huawei(){
- @Override
- public String getMarketUrl() {
- return "http://play.huawei.com";//huawei market url
- }
- },ZTE(){
- @Override
- public boolean isShouldAd(){
- return false;
- }
- @Override
- public String getMarketUrl() {
- return "http://play.zte.com";//ZTE market url
- }
- },OneTouch(){
- @Override
- public String getMarketUrl() {
- return "http://play.huawei.com";
- }
- };
- public boolean isShouldAd(){
- return true;
- }
- public String getMarketUrl(){
- return "http:\\googleplay....";//google play url
- }
- }
- public enum Market {
- Default,Huawei(){
- @Override
- public String getMarketUrl() {
- return "http://play.huawei.com";//huawei market url
- }
- },ZTE(){
- @Override
- public boolean isShouldAd(){
- return false;
- }
- @Override
- public String getMarketUrl() {
- return "http://play.zte.com";//ZTE market url
- }
- },OneTouch(){
- @Override
- public String getMarketUrl() {
- return "http://play.huawei.com";
- }
- };
- public boolean isShouldAd(){
- return true;
- }
- public String getMarketUrl(){
- return "http:\\googleplay....";//google play url
- }
- }
通过上面的例子就大概了解了一些java枚举在android的基本使用。为了了解java枚举的原理,我写了一个很常用的红绿灯例子。下面是用枚举的代码:
- public enum TrafficLight {
- red(45) {
- @Override
- public TrafficLight nextLamp() {
- return green;
- }
- },
- green(30) {
- @Override
- public TrafficLight nextLamp() {
- return yellow;
- }
- },
- yellow(3) {
- @Override
- public TrafficLight nextLamp() {
- return red;
- }
- };
- private int time;
- private TrafficLight(int time) {
- this.time = time;
- };
- public abstract TrafficLight nextLamp();
- public int getTime() {
- return this.time;
- }
- }
- public enum TrafficLight {
- red(45) {
- @Override
- public TrafficLight nextLamp() {
- return green;
- }
- },
- green(30) {
- @Override
- public TrafficLight nextLamp() {
- return yellow;
- }
- },
- yellow(3) {
- @Override
- public TrafficLight nextLamp() {
- return red;
- }
- };
- private int time;
- private TrafficLight(int time) {
- this.time = time;
- };
- public abstract TrafficLight nextLamp();
- public int getTime() {
- return this.time;
- }
- }
- public abstractclass TrafficLight {
- public staticfinal TrafficLight red =new TrafficLight(45){
- @Override
- public TrafficLight nextLamp() {
- return green;
- }
- };
- public staticfinal TrafficLight green =new TrafficLight(30) {
- @Override
- public TrafficLight nextLamp() {
- return yellow;
- }
- };
- public staticfinal TrafficLight yellow =new TrafficLight(3) {
- @Override
- public TrafficLight nextLamp() {
- return red;
- }
- };
- private int time;
- private TrafficLight(int time) {
- this.time = time;
- };
- public abstract TrafficLight nextLamp();
- public int getTime() {
- return this.time;
- }
- }
- public abstractclass TrafficLight {
- public staticfinal TrafficLight red = new TrafficLight(45){
- @Override
- public TrafficLight nextLamp() {
- return green;
- }
- };
- public staticfinal TrafficLight green = new TrafficLight(30) {
- @Override
- public TrafficLight nextLamp() {
- return yellow;
- }
- };
- public staticfinal TrafficLight yellow = new TrafficLight(3) {
- @Override
- public TrafficLight nextLamp() {
- return red;
- }
- };
- private int time;
- private TrafficLight(int time) {
- this.time = time;
- };
- public abstract TrafficLight nextLamp();
- public int getTime() {
- return this.time;
- }
- }
这些都是enum的一些基本应用。然后是我今天在项目如何应用用枚举的类型的。因为我们那个app有三个不同的请求数据的url。其实我们只有一个数据源,如果find不到,就会通过其他两个是读取其他网站html,然后解析,通过正则表达式匹配得到数据。每个数据源需要设置httpClient、httpGet、httpResponse等参数,然后使用了枚举。我这里贴出一点基本的。然后发现其实都是差不多的。
- 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() {
- @Override
- public String getUrl(String keyWord) {
- return "http://..." + keyWord +"...";
- }...
- },
- google() {
- @Override
- public String getUrl(String keyWord) {
- return "http://google..." + keyWord +"...";
- }...
- },
- amazon() {
- @Override
- public String getUrl(String keyWord) {
- return "http://amazon..." + keyWord +"...";
- }...
- };
- public abstract String getUrl(String keyWord);
- public 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(get);
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return res;
- }...
- }
0 0
- java枚举在android项目应用
- java枚举在android项目应用
- java枚举在android项目应用
- java枚举在android项目应用
- 枚举在java项目中的应用
- C# 枚举在项目中的应用总结
- Java 数据类型在实际开发中应用二枚举
- JAVA枚举的应用
- Java枚举应用方法
- Java枚举的应用
- Java枚举的应用
- Java枚举应用实例
- java枚举应用(一)
- java枚举应用(二)
- java枚举应用(三)
- JAVA 枚举基础应用
- Java-枚举应用总结
- Java简单枚举应用
- 3.Swift教程翻译系列——Swift基础知识
- java枚举在android项目应用
- poj 2718 Smallest Difference
- js 阻止元素(文档)默认事件,阻止冒泡事件
- 移动终端,一个凑单的想法
- java枚举在android项目应用
- ubuntu----视频播放软件 VLC
- linux UDP通讯 获取源IP地址端口信息
- 做一个不乖的孩子
- 关于使用.Net4的web应用在mono上呈现目录结构的解决办法
- 用减奇数次数的方法,求一个数的近似平方根
- 如果看了这篇文章你还不懂傅里叶变换,那就过来掐死我吧
- With KEY & With Table KEY 的使用
- 编译插件到cocosbuilder工程中