麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 系統(tǒng) > Android > 正文

Android開發(fā)筆記之:用Enum(枚舉類型)取代整數(shù)集的應(yīng)用詳解

2020-04-11 12:22:55
字體:
供稿:網(wǎng)友
在Android的API中可以發(fā)現(xiàn)有很多用整數(shù)集來作為參數(shù)的地方,先來看一下實(shí)例。
LinearLayout是大家所熟知的一個(gè)UI基本元素,它里面有一個(gè)方向的屬性,可以通過以下方法來設(shè)置:
復(fù)制代碼 代碼如下:

LinearLayout.setOrientation(int);

使用的時(shí)候,通常都是這樣:
復(fù)制代碼 代碼如下:

LinearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.setOrientation(LinearLayout.VERTICAL);

但也可以這樣使用:
復(fù)制代碼 代碼如下:

LinearLayout.setOrientation(0); // LinearLayout.HORIZONTAL = 0
LinearLayout.setOrientation(1); // LinearLayout.VERTICAL = 0x01

甚至可以這樣:
復(fù)制代碼 代碼如下:

LinearLayout.setOrientation(Integer.MAX_VALUE);
LinearLayout.setOrientation(Integer.MIN_VALUE);
LinearLayout.setOrientation(2012);

因?yàn)榉椒╯etOrientation接收的參數(shù)是一個(gè)整數(shù),所以你可以傳任意合法的整數(shù)---至少這在編譯時(shí)不會(huì)有任何問題。它只會(huì)在運(yùn)行時(shí)可能引發(fā)問題,但如你所知,開發(fā)者只關(guān)注程序能否編譯成功,至于運(yùn)行時(shí),那是用戶關(guān)心的事兒,因?yàn)殚_發(fā)者不一定使用他們所開發(fā)出的程序。

除了這個(gè)例子,在Android的API中到處可以看到這種API,比如設(shè)置View的可見性,設(shè)置Wifi狀態(tài)等等。都是定義了整數(shù)集,然后用整數(shù)來做為參數(shù),并寄希望開發(fā)者能傳遞整數(shù)集中定義的常量來作為參數(shù)。但如你所知,并不是每個(gè)人都那么的守規(guī)矩,如果每個(gè)人都能遵守規(guī)則,這個(gè)世界就真的和諧了,蛋扯遠(yuǎn)了。
因?yàn)殚_發(fā)者通常只能關(guān)注編譯,所以如果能把這個(gè)規(guī)則應(yīng)用在編譯時(shí),那么就會(huì)大大減少出錯(cuò)的可能。有興趣的朋友可以去試試看,給這些接收整數(shù)參數(shù)的方法傳一些“平常”的數(shù)值,比如2012,Integer.MAX_VALUE,Integer.MIN_VALUE等等,看會(huì)出現(xiàn)什么狀況。
另外,如果開發(fā)者傳遞與常量定義一致的整數(shù)值,雖然編譯運(yùn)行都不會(huì)有錯(cuò),但代碼的可讀性會(huì)大大的降低,比如:
復(fù)制代碼 代碼如下:

LinearLayout.setOrientation(0);
LinearLayout.setOrientation(1);

這完全沒有錯(cuò),但是代碼的閱讀者和維護(hù)者通常都會(huì)蛋疼的。
當(dāng)然,Android自身還是有保護(hù)措施的,如果對(duì)API傳遞不合法參數(shù),不會(huì)造成其他影響,只是設(shè)置不能生效,但API會(huì)使用默認(rèn)值,因?yàn)閷?duì)于每個(gè)內(nèi)置參數(shù),都有相應(yīng)的默認(rèn)值。如LinearLayout的orientation,默認(rèn)值就是LinearLayout.HORIZONTAL,所以如果對(duì)setOrientation()傳入非法值,LinearLayout會(huì)保持水平排列,無其他影響。后面有個(gè)對(duì)Linearlayout的orientation做的試驗(yàn)。
另外,如果在Layout XML文件中設(shè)置這些屬性就不會(huì)有些問題,如:
復(fù)制代碼 代碼如下:

<LinearLayout
android:orientation="vertical"
android:gravity="center">

因?yàn)閄ML布局會(huì)在編譯時(shí)被處理,如果有非法的值,會(huì)有編譯錯(cuò)誤的。我想這也就是Android特別鼓勵(lì)開發(fā)者用XML來制作所有的布局的一個(gè)原因吧。實(shí)例,三個(gè)沒有設(shè)置指向的線性布局,默認(rèn)是水平放置,在代碼中設(shè)置了幾個(gè)離譜的值,發(fā)現(xiàn)它們還是水平的,也就是說設(shè)置離譜的值不會(huì)出錯(cuò),但也不起作用:運(yùn)行結(jié)果如下:


代碼如下:

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
    <LinearLayout
        android:id="@+id/linearlayout_test_1"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">

   <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="#ff00ff00"
     android:background="#aa331155"
     android:layout_weight="1"
     android:textSize="18sp"
     android:text="Microsoft"
        />
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffff0000"
        android:background="#aa117711"
        android:layout_weight="1"
        android:textSize="18sp"
        android:text="Apple"
        />
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff0000ff"
        android:background="#aa774411"
        android:layout_weight="1"
        android:textSize="18sp"
        android:text="Google"
        />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout_test_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="#ff00ff00"
           android:background="#aa331155"
           android:layout_weight="1"
           android:textSize="18sp"
           android:text="Microsoft"
           />
      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="#ffff0000"
           android:background="#aa117711"
           android:layout_weight="1"
           android:textSize="18sp"
           android:text="Apple"
           />
      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="#ff0000ff"
           android:background="#aa774411"
           android:layout_weight="1"
           android:textSize="18sp"
           android:text="Google"
           />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout_test_3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="#ff00ff00"
           android:background="#aa331155"
           android:layout_weight="1"
           android:textSize="18sp"
           android:text="Microsoft"
           />
      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="#ffff0000"
           android:background="#aa117711"
           android:layout_weight="1"
           android:textSize="18sp"
           android:text="Apple"
           />
      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textColor="#ff0000ff"
           android:background="#aa774411"
           android:layout_weight="1"
           android:textSize="18sp"
           android:text="Google"
           />
    </LinearLayout>
</LinearLayout>

和:
復(fù)制代碼 代碼如下:

package com.android.explorer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class LinearLayoutTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout_test);
        LinearLayout one = (LinearLayout) findViewById(R.id.linearlayout_test_1);
        one.setOrientation(2012);
        LinearLayout two = (LinearLayout) findViewById(R.id.linearlayout_test_2);
        two.setOrientation(Integer.MAX_VALUE);
        LinearLayout three = (LinearLayout) findViewById(R.id.linearlayout_test_3);
        three.setOrientation(Integer.MIN_VALUE);
    }
}

用Enum代替整數(shù)集
其實(shí)很簡(jiǎn)單,用Enum(枚舉)就可以很方便的解決這個(gè)問題,使用起來也不比定義整數(shù)集繁瑣,同樣的可讀。另外的優(yōu)點(diǎn)就是,它的封裝更好,最重要的是它會(huì)在編譯時(shí)被檢查。因?yàn)镴ava是一種Strong Type,也就是說在編譯時(shí),編譯器會(huì)對(duì)所有原型類型和參數(shù)類型進(jìn)行檢查,如果類型不對(duì),并且沒有強(qiáng)制轉(zhuǎn)型的,就會(huì)報(bào)出編譯錯(cuò)誤,當(dāng)然編譯器所支持的自動(dòng)轉(zhuǎn)型除外。比如一個(gè)需要int,而傳的參數(shù)是long,雖然都差不多,沒有溢出等,但還是會(huì)有編譯錯(cuò)誤。
所以,如果LinearLayout使用Enum,就像這樣定義:
復(fù)制代碼 代碼如下:

public class LinearLayout extends ViewGroup {
    private Orientation mOrientation;

    public enum Orientation {
        HORIZONTAL, VERTICAL
    };

    public void setOrientation(Orientation dir) {
        mOrientation = dir;
    }
}

然后這樣使用:
復(fù)制代碼 代碼如下:

import android.widget.LinearLayout;
LinearLayout.setOrientation(Orientation.HORIZONTAL);
LinearLayout.setOrientation(Orientation.VERTICAL);

那么,開發(fā)者就不會(huì)用錯(cuò)了,因?yàn)槭紫龋吹絪etOrientation所需要的參數(shù)是一個(gè)Orientation的枚舉類型,就會(huì)自然的傳送Orientation中定義的類型;另外,如果傳其他的值,比如0或者1,編譯器也不會(huì)答應(yīng)的。

可悲的是Android中幾乎所有的API都是以整數(shù)集的方式來定義的,所以就要時(shí)刻提醒自己和組里的人,一定要傳所定義的整數(shù)集中的常量。
那么我們能做的,除了要傳整數(shù)集中定義的常量,對(duì)于那些以整數(shù)集方式定義的API,以外。更重要的是當(dāng)自己定義接口的時(shí)候,盡量用Enum而不要使用整數(shù)集。
還有一點(diǎn)需要注意的是,對(duì)于某些弱類型語(yǔ)言,也就是說在編譯時(shí)不會(huì)對(duì)類型做特別細(xì)致的檢查,比如C++,C等,那么即使使用了Enum,也不一定安全,因?yàn)閷?duì)于C++和C來講Enum中的常量與整數(shù)常量完全一樣,連編譯器都分不清。所以,對(duì)于這類語(yǔ)言,只能寄希望于開發(fā)者了。
后記:
寫完這篇,讓我想起了另外一些與參數(shù)定義相關(guān)的問題,比如布爾型參數(shù)也不是一個(gè)很好的設(shè)計(jì),因?yàn)槭褂谜吆茈y到底應(yīng)該傳True還是傳False,特別是當(dāng)方法名字不能體現(xiàn)Boolean參數(shù)作用時(shí)和文檔不夠清楚的時(shí)候。如果只有一個(gè)參數(shù)還好,根據(jù)方法名字和常識(shí)都能知道,比如:
復(fù)制代碼 代碼如下:

Button.setEnabled(true); // enable the button
Button.setEnabled(false); // disable the button

但對(duì)于某些情況,當(dāng)方法的名字不能體現(xiàn)Boolean參數(shù)的作用時(shí),或是多于一個(gè)參數(shù)時(shí),而方法的主要目的又不能體現(xiàn)Boolean參數(shù)的作用時(shí),就很不清楚,比如:
復(fù)制代碼 代碼如下:

// com/android/mms/data/ContactList.java
public String[] getNumbers(boolean);

您能猜出來這個(gè)boolean變量是決定是否要為彩信對(duì)聯(lián)系人做特殊的處理嗎?您在使用這個(gè)API的時(shí)候能很快知道該傳True還是該傳False嗎?當(dāng)讀到這些語(yǔ)句的時(shí)候:
復(fù)制代碼 代碼如下:

String[] mms = getNumbers(true);
String[] sms = getNumbers(false);

您能知道True和False的含義與作用嗎?至少我看到這樣的代碼時(shí),如果不去跟蹤它的實(shí)現(xiàn),是猜不出來的。
但現(xiàn)實(shí)的問題是,API通常又需要從調(diào)用者那里得到做還是不做的決定。一個(gè)可行的途徑是用方法來封裝和隱藏,比如:
復(fù)制代碼 代碼如下:

Button.setEnabled(true); // enable the button
Button.setEnabled(false); // disable the button

可以改成:
復(fù)制代碼 代碼如下:

Button.enable();
Button.disable();

這是簡(jiǎn)單的情況,對(duì)于稍復(fù)雜的情況,比如后一個(gè)例子,可以添加另外的接口,而不是用重載方法,但內(nèi)部的實(shí)現(xiàn),可能還是需要重載,但是這就把問題縮小了,起碼對(duì)使用者來說是隱藏的:
復(fù)制代碼 代碼如下:

// com/android/mms/data/ContactList.java
public String[] getNumbersForSms();
public String[] getNumbersForMms();

這樣一來,對(duì)外來講就是良好的封裝。內(nèi)部實(shí)現(xiàn)可能還是需要一個(gè)類似這樣的私有方法:
復(fù)制代碼 代碼如下:

// com/android/mms/data/ContactList.java
public String[] getNumbersForSms() {
   return getNumbers(false);
}
public String[] getNumbersForMms() {
   return getNumbers(true);
}
private String[] getNumbers(boolean) {
   // implementation
}

但至少把問題縮小化了,也可以加上注釋來說明。就不必導(dǎo)致使用者來猜方法的用法和含義了。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费一级欧美在线观看视频 | 欧美一区二区黄 | 亚洲国产综合在线观看 | 黄色影院在线看 | 91麻豆精品国产91久久久点播时间 | 国产欧美亚洲精品a | 国产精品久久久久久影院8一贰佰 | 欧美黄色一级片在线观看 | 狠狠色噜噜狠狠狠米奇9999 | 制服丝袜成人动漫 | 宅男视频在线观看免费 | 国产激情视频在线 | 欧美日韩大片在线观看 | 女人一级一级毛片 | 欧美a级在线免费观看 | 亚洲国产色婷婷 | 亚洲第一成人av | 久久久久久久久久91 | 精品国产一区二区三区成人影院 | 色柚视频网站ww色 | av电影在线观看网站 | 欧美午夜网 | 一级做受毛片免费大片 | 免费视频aaa | 国产一级爱c视频 | 黄色网址免费入口 | 久草在线观看福利视频 | 久草成人在线 | 欧美一级高潮 | 久久蜜臀一区二区三区av | 久久在线免费视频 | 欧美精品在线免费观看 | 国产毛片毛片 | 成人午夜在线免费 | 91九色国产视频 | 亚洲网视频 | 一级免费观看 | 国产二区三区在线播放 | 久久久久一区二区三区四区五区 | 黑人一区二区三区四区五区 | 免费国产之a视频 |