Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Java枚举在Android项目应用

今天修复一个公司很早以前的Android应用功能,里面的代码逻辑已经完全错乱,然后发现返回的数据完全不对了。然后修复了整整两天。然后我重新整理了一遍,重构就算不上了。然后就用上了枚举。什么是枚举?我以前也不懂,当时我看见公司的项目中使用了枚举当做项目一个控制,比如修改已经写好的app然后为一些手机厂商做定制版。可能要去掉广告,还有跳转到商店url都不同,特别是国内基本都没有google play。我们为了避免以后的修改,就会写个枚举来控制它。Ubuntu 14.04 x64配置Android 4.4 kitkat编译环境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htmUbuntu 12.04搭建Android开发环境 http://www.linuxidc.com/Linux/2012-09/69961.htmUbuntu 14.04 配置 Android SDK 开发环境 http://www.linuxidc.com/Linux/2014-05/101039.htm64位Ubuntu 11.10下Android开发环境的搭建(JDK+Eclipse+ADT+Android SDK详细) http://www.linuxidc.com/Linux/2013-06/85303.htmUbuntu 12.10 x64 安装 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htmJava枚举在Struts2中的应用  http://www.linuxidc.com/Linux/2012-02/53808.htmpublic enum Market {
 
 Default,linuxidc(){
  @Override
  public String getMarketUrl() {
   return "http://play.linuxidc.com";//linuxidc market url
  }
 },androidj(){
  @Override
  public boolean isShouldAd(){
   return false;
  }
  @Override
  public String getMarketUrl() {
   return "http://play.androidj.com";//androidj market url
  }
 },OneTouch(){
  @Override
  public String getMarketUrl() {
   return "http://play.linuxidc.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;
 }
}然后是普通class模拟enum的代码:public abstract class TrafficLight {
  public static final TrafficLight red  = new TrafficLight(45){
  @Override
  public TrafficLight nextLamp() {
   return green;
  }
 };
 public static final TrafficLight green  = new TrafficLight(30) {
  @Override
  public TrafficLight nextLamp() {
   return yellow;
  }
 };
 
 public static final 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;
 }
}更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-06/103065p2.htm