我目前正在为一个项目开发我的第一个应用程序,并想知道应该使用对象并且代码少或者代码多但没有对象。 这里是代码将进入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