使用实例变量与将数据作为参数传递

Use of instance variables vs passing data as a parameter

使用实例变量而不是将数据作为参数传递时有什么缺点。

使用实例变量对我来说似乎更具可读性,但是让它们对整个系统可用有缺点吗class。我想这是从全局变量和局部变量的角度来看的。

使用实例变量

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]);
        }
    }   
}

第二个示例将数据作为参数传递

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]);
        }
    }   
}

使用参数可以使方法自包含。你永远不必想知道实例变量有什么值,因为你把数据作为参数传递了。

除了封装的非常有用的好处之外,参数化方法比依赖于实例变量值的方法更容易进行单元测试。