我目前正在爲一個項目開發我的第一個應用程序,並想知道應該使用對象並且代碼少或者代碼多但沒有對象。 這裏是代碼將進入4個不同的方法(我每個活動)或一個類在每個活動中引用。以上這Android開發:是否在使用對象不好的做法?
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不好的做法。 非常感謝,如果它非常模糊,但它太多的代碼張貼=)感到抱歉。
使用循環嘗試,這將節省alots重複。 – Shang
「使用對象」通常是一個非常好的主意 - 它有助於保持以邏輯和可管理的方式分組的特定任務的代碼 - 但這不是您需要的;你需要一個數組。 – chrylis
不要低估這個人,他提出了一個合法的問題。每個人都是初學者。 – rupps