[英]Use of instance variables vs passing data as a parameter
What are the disadvantages when an instance variable is used instead of passing the data as a parameter.当使用实例变量而不是将数据作为参数传递时有什么缺点。
Using instance variables seems more readable to me, but are there disadvantages of making them available to the whole class.使用实例变量对我来说似乎更具可读性,但是将它们提供给整个类是否存在缺点。 I guess this is coming from global vs local variable perspective.
我想这是从全局变量与局部变量的角度来看的。
Using instance variables使用实例变量
public class ChoicesFragment extends Fragment implements View.OnClickListener {
private CharSequence[] mButtonTextData;
private String mTitleTextData;
private View mUserChoiceView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mUserChoiceView = inflater.inflate(R.layout.available_choices, container, false);
getBundleData();
setTitle();
setButtons();
return mUserChoiceView;
}
private void getBundleData() {
//populate button text
Bundle bundleData = getArguments();
mTitleTextData = bundleData.getString(CardFragment.TITLE_TEXT_KEY);
mButtonTextData = bundleData.getCharSequenceArray(CardFragment.BUTTON_TEXT_KEY);
}
private void setTitle() {
TextView title = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView1);
String genericTitle = getResources().getString(R.string.title_selection);
title.setText(genericTitle + mTitleTextData);
}
//sets up button data
public void setButtons() {
TextView[] buttonsTextView = new TextView[4];
buttonsTextView[0] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView2);
buttonsTextView[1] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView3);
buttonsTextView[2] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView4);
buttonsTextView[3] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView5);
for(int i = 0; i < buttons.length; i++) {
buttonsTextView[i].setText(mButtonTextData[i]);
}
}
}
Second example pass the data as parameters第二个示例将数据作为参数传递
public class ChoicesFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View userChoiceView;
String titleTextData;
CharSequence[] buttonTextData;
mUserChoiceView = inflater.inflate(R.layout.available_choices, container, false);
Bundle bundleData = getArguments();
buttonTextData = bundleData.getString(CardFragment.TITLE_TEXT_KEY);
titleTextData = bundleData.getCharSequenceArray(CardFragment.BUTTON_TEXT_KEY);
setTitle(userChoiceView, titleTextData);
setButtons(userChoiceView, buttonTextData);
return mUserChoiceView;
}
private void setTitle(View userChoiceView, String titleText) {
TextView title = (TextView) userChoiceView.findViewById(R.id.choicesTextView1);
String genericTitle = getResources().getString(R.string.title_selection);
title.setText(genericTitle + titleText);
}
//sets up button data
public void setButtons(View userChoiceView, CharSequence[] buttonText) {
TextView[] buttonsTextView = new TextView[4];
buttonsTextView[0] = (TextView) userChoiceView.findViewById(R.id.choicesTextView2);
buttonsTextView[1] = (TextView) userChoiceView.findViewById(R.id.choicesTextView3);
buttonsTextView[2] = (TextView) userChoiceView.findViewById(R.id.choicesTextView4);
buttonsTextView[3] = (TextView) userChoiceView.findViewById(R.id.choicesTextView5);
for(int i = 0; i < buttons.length; i++) {
buttonsTextView[i].setText(buttonText[i]);
}
}
}
Using parameters allows you to make the method self-contained.使用参数可以使方法自包含。 You never have to wonder what value the instance variable has, because you passed the data as a parameter.
您永远不必想知道实例变量具有什么值,因为您将数据作为参数传递。
Apart from the very useful benefit of encapsulation, parameterized methods are far easier to unit test than methods that depend on the value of instance variables.除了封装的非常有用的好处之外,参数化方法比依赖于实例变量值的方法更容易进行单元测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.