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

时间:2015-03-28 19:11:10

标签: java android performance coding-style

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

使用实例变量对我来说似乎更具可读性,但它们的缺点是使它们可供全班使用。我想这是来自全球与局部变量的观点。

使用实例变量

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

1 个答案:

答案 0 :(得分:3)

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

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