枚举类型(enum)

今天在查看android 计算器源码的过程中发现,谷歌的程序员在定义计算器的工作状态时,使用的是一个枚举类型

private enum CalculatorState {    INPUT, EVALUATE, RESULT, ERROR}

最初我对枚举类型的理解仅仅是一个表达选择内容的类型,因为可以使用private static final int ... = ...的形式代替,也就没有细看,今天系统的学习一下;

枚举类型简单的应用场景

在类的对象有限且固定的时候,可以使用枚举类,比如说:

private enum SexEnumTest {    MALE,FEMAL}/*private static final int MALE = 1;private static final int FEMALE = 2;*/private enum SeasonEnumTest {    SPRING,SUMMER,FALL,WINTER}/*private static final int SPRING = 1;private static final int SUMMER = 2;private static final int FALL = 3;private static final int WINTER = 4;*/

比如说当前判断完小明是男生或者当前季节是春天时,我们可以对上述枚举类型中的值进行一个get方法,这样的定义快速有效并且简洁、可读性高

public enum SexEnum {  MALE("male"),FEMALE("female");  private final String strings;  SexEnum(String strings){    this.strings = strings;  }  public String getStrings(){    return this.strings;  }}public enum SeasonEnum {    SPRING("spring"),SUMMER("summer"),FALL("fall"),WINTER("winter");    private final String nowSeason;    SeasonEnum (String s){        this.nowSeason = s;    }    public String getNowSeason(){        return this.nowSeason;    }}public static void main(String[] args) {     SexEnum xiaoMing = SexEnum.MALE;     System.out.println(xiaoMing.getStrings());     SeasonEnum nowSeason = SeasonEnum.SPRING;     System.out.println(nowSeason.getNowSeason()); }

枚举类型的简单使用实例

下面我们模拟一个android中经常遇到的一个情况:
在主界面上有一个TextView,三个Button,初始情况TextView中显示的是"init状态",当用户点击Button时Toast会提示依次改变显示状态"red"“green”"blue"要求涉及到枚举类型,使用Button的点击事件去设置当前的显示状态.
界面布局文件的代码:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    android:orientation="vertical">    <TextView        android:id="@+id/showText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="init"        android:textSize="80sp"        android:layout_gravity="center"        android:layout_marginTop="30dp"        android:textColor="@color/colorPrimaryDark" />    <Button        android:id="@+id/btnRed"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"        android:text="Red"        android:textSize="30sp"        android:layout_margin="30dp"        android:onClick="changeState"/>    <Button        android:id="@+id/btnBlue"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"        android:text="Blue"        android:textSize="30sp"        android:layout_marginLeft="30dp"        android:layout_marginRight="30dp"        android:onClick="changeState"/>    <Button        android:id="@+id/btnGreen"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"        android:text="Green"        android:textSize="30sp"        android:layout_marginTop="30dp"        android:layout_marginLeft="30dp"        android:layout_marginRight="30dp"        android:onClick="changeState"/>LinearLayout>

主函数部分的代码:

import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    public colorState nowState;    public TextView showText;    public Button btnRed;    public Button btnGreen;    public Button btnBlue;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bind();        init();    }    public void changeState(View view) {        //点击事件        switch (view.getId()){            case R.id.btnBlue:                setNowState(colorState.BLUE);                Toast.makeText(this,"change state to blue", Toast.LENGTH_LONG).show();                break;            case R.id.btnGreen:                setNowState(colorState.GREEN);                Toast.makeText(this, "change state to green",Toast.LENGTH_LONG).show();                break;            case R.id.btnRed:                setNowState(colorState.RED);                Toast.makeText(this, "change state to red", Toast.LENGTH_LONG).show();                break;            default:                break;        }    }    private enum colorState{        //枚举类        INIT,RED,GREEN,BLUE    }    public void setNowState(colorState State) {        switch (State){            case RED:                nowState = colorState.RED;                showText.setText("Red");                break;            case BLUE:                nowState = colorState.BLUE;                showText.setText("Blue");                break;            case INIT:                nowState = colorState.INIT;                break;            case GREEN:                nowState = colorState.GREEN;                showText.setText("Green");                break;            default:                break;        }    }    public void bind(){        //查询控件        showText = findViewById(R.id.showText);        btnBlue = findViewById(R.id.btnBlue);        btnGreen = findViewById(R.id.btnGreen);        btnRed = findViewById(R.id.btnRed);    }    public void init(){        //初始化        setNowState(colorState.INIT);        showText.setText("Init");    }}

包含抽象方法的枚举类型

掌握上述的例子,这部分就很容易理解了,我们在创建枚举类型时,除了可以设置枚举类型对应的值,也可以设置对应的方法,例如:

public enum meetEnum {    xiaofang {        public String meet(String place){            return "xiao ming meet xiao fang in " + place;        }    },    xiaohong {        public String meet(String place){            return "xiao ming meet xiao hong in " + place;        }    };    public abstract String meet(String place);}public class EnumTest {    public static void main(String[] args) {        System.out.println(meetEnum.xiaofang.meet("School"));        System.out.println(meetEnum.xiaohong.meet("Street"));    }}//控制台打印信息:xiao ming meet xiao fang in Schoolxiao ming meet xiao hong in Street

这里注意需要使用abstract关键字定义一个抽象方法,才可以在主函数中调用。定义枚举类型时不可以使用abstract关键字,因为枚举类型需要显示创建,所以定义每个枚举值时都必须为抽象方法提供实现!

实现接口的枚举类型

上述表面枚举类型是可以实现方法的,那么也就是说枚举类型可以实现接口方法。这里我也就不多说了,无非就是上述例子中的meet(String Place)函数成为了某个接口定义的方法.

更多相关文章

  1. android webview自定义标签!(实现打电话的功能);
  2. android TextView自定义字体样式
  3. Android 中自定义权限
  4. Android 解决65535的限制 使用android-support-multidex解决Dex
  5. Android APK开发 Drawable文件夹下的自定义Drawable文件
  6. Android 应用语言切换的三种方法
  7. Android CheckBox中设置padding无效问题解决方法
  8. Android alertdialog的自定义添加按钮和添加点击
  9. Android 自定义SeekBar样式

随机推荐

  1. Android入门教程(二)之------环境搭建
  2. android EditText中的inputType
  3. 安卓巴士总结了近百个Android优秀开源项
  4. 简单 4部 完成 android 二维码扫一扫功能
  5. Android中扫描多媒体文件操作详解
  6. Android的源代码结构
  7. Android(安卓)逆向apk的配置文件
  8. android重要包的描述
  9. Android(安卓)- 文件读写操作 总结
  10. Android开机动画bootanimation.zip