gpt4 book ai didi

java - 安卓开发: Is using objects bad practice?

转载 作者:行者123 更新时间:2023-12-01 23:05:38 24 4
gpt4 key购买 nike

我目前正在为一个项目开发我的第一个应用程序,并且想知道我应该使用对象并使用更少的代码还是使用更多的代码但没有对象。下面的代码将进入 4 个单独的方法(每个 Activity )或每个 Activity 中引用的一个类。

        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 个不同的 Activity 中,因为我正在加载根据教师而变化的评分方案。我目前将它放在一个单独的类中,并且我正在创建该类的一个对象来调用加载表的方法,即上面的代码,或者我应该将此代码单独放在我的 Activity 中,因为我听说使用对象很糟糕实践。提前致谢,如果它非常模糊,那么抱歉,但它的代码太多,无法发布 =)。

最佳答案

您可以使用循环来简化它,例如:

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(....)
}

关于java - 安卓开发: Is using objects bad practice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com