簡體   English   中英

Android開發:使用對象是一種不好的做法嗎?

[英]Android Development: Is using objects bad practice?

我目前正在為一個項目開發我的第一個應用程序,想知道我應該使用對象,代碼較少還是有更多代碼卻沒有對象。 這是將進入4個獨立方法(每個活動i)或在每個活動中引用一個類的代碼。

        TextView text1 = (TextView)this.cal.findViewById(R.id.txt1);
        TextView text2 = (TextView)this.cal.findViewById(R.id.txt2);
        TextView text3 = (TextView)this.cal.findViewById(R.id.txt3);
        TextView text4 = (TextView)this.cal.findViewById(R.id.txt4);
        TextView text5 = (TextView)this.cal.findViewById(R.id.txt5);
        TextView text6 = (TextView)this.cal.findViewById(R.id.txt6);
        TextView text7 = (TextView)this.cal.findViewById(R.id.txt7);
        TextView text8 = (TextView)this.cal.findViewById(R.id.txt8);
        TextView text9 = (TextView)this.cal.findViewById(R.id.txt9);
        TextView text10 = (TextView)this.cal.findViewById(R.id.txt10);
        TextView text11 = (TextView)this.cal.findViewById(R.id.txt11);
        if(x == 0)
        {
            text1.setText (text1.getText() + "sciemce and enginnering");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");

        }
        else if(x ==1)
        {
            text1.setText (text1.getText() + "arts");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");
        }

        else if(x == 2)
        {
            text1.setText (text1.getText() + "1");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");

        }

        else if(x ==3)
        {
            text1.setText (text1.getText() + "1");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");
        }





}

上面的這段代碼很長(如果有4條if語句,將持續很長時間),它將在4種不同的活動中出現,因為我正在加載根據學院而變化的評分方案。 目前,我將其放在單獨的類中,並且正在創建該類的對象以調用方法來加載上面的代碼表,或者由於我聽說使用ojects不好,所以應該將該代碼分別放入我的活動中實踐。 非常感謝您,如果模棱兩可但很抱歉,但是代碼太多,無法發布=)。

您可以使用循環來簡化它,例如:

else if(x ==1)
    {
        text1.setText (text1.getText() + "arts");
        text2.setText (text2.getText() + "add a bit");
        text3.setText (text3.getText() + "add a bit");
        text4.setText (text4.getText() + "add a bit");
        text5.setText (text5.getText() + "add a bit");
        text6.setText (text6.getText() + "add a bit");
        text7.setText (text7.getText() + "add a bit");
        text8.setText (text8.getText() + "add a bit");
        text9.setText (text9.getText() + "add a bit");
        text10.setText (text10.getText() + "add a bit");
        text11.setText(text11.getText() + "add a bit");
    }

for (int i = 1; i < 12; i++){
    text[i].setText(....)
}

您可以在這里做很多事情。 跳到我身上的第一件事是:x的值是什么意思? 當您檢查x == 0時,該條件代表什么? 我將“ x”重命名為更具描述性的名稱,並可能在具有硬編碼整數的位置創建命名常量。 因此,您的代碼將類似於

if (selectedDegree == SCIENCE_AND_ENGINEERING)

代替

if (x == 0)

任何閱讀您的代碼的人都將更加了解它。 我還將創建一個輔助方法來每次附加文本。

text1.setText (text1.getText() + "sciemce and enginnering");
text2.setText (text2.getText() + "add a bit");
text3.setText (text3.getText() + "add a bit");
text4.setText (text4.getText() + "add a bit");

會成為

appendText(text1, "science and engineering");
appendText(text2, "and a bit");
appendText(text3, "and a bit");
appendText(text4, "and a bit");

private void appendText(TextView textView, String addedText) {
    if (textView != null) {
        textView.setText(textView.getText() + addedText);
    }
}
  1. 一張桌子? 如果沒有太多基於id的工作,請嘗試使用List而不是很多對象,就像這樣。

     list.add(this.cal.findViewById(...)) 
  2. 並更改if ... else ..進行切換;

  3. 使用循環是完全可以的。

     for(item in list){ if(x==1 && index==0) {item.setText("...");} else{item.setText("another...");} } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM