public MyObject getMSListItem(int pos) {
MyObject li = getItem(pos);
return li;
}
现在看来,我想,我可以这样做来解决它:
public MyObject getMSListItem(int pos) {
return getItem(pos);
}
另一个例子:
public String getTeacher(int pos) {
ffTeacherListItem t = getItem(pos);
String teacher = t.teacher;
return teacher;
}
看来这可能是:
public String getTeacher(int pos) {
ffTeacherListItem t = getItem(pos);
return t.teacher;
}
或者按照下面的建议,甚至更好!
public String getTeacher(int pos) {
return getItem(pos).teacher;
}
这真的有“最佳实践”吗?一种方法比另一种方法更好吗?或者只是为了代码的可读性而仅此而已?
这真的有“最佳实践”吗?是一种比
其他?或者只是为了代码的可读性而仅此而已?
简化说:在你的场景中没用的。这并没有错,但你为什么要这样做:
ffTeacherListItem t = getItem(pos);
String teacher = t.teacher;
return teacher;
当你可以做同样的事情时:
ffTeacherListItem t = getItem(pos);
return t.teacher;
或者你也可以这样做:
return getItem(pos).teacher;
All above do same but second and third code is cleaner and you should always try to write clean code without useless lines and references1. There is also unwritten rule - Less code, less errors.
1This is "advantage" of languages like C++ which don't have garbage collector and you are responsible for all objects and instances you'll create (their releasing from memory etc.). So you are thinking more before you'll decide to create new instance of some Object.