【问题标题】:How come the same variable can be used for 3 different things?为什么同一个变量可以用于 3 种不同的事情?
【发布时间】:2016-09-05 20:06:58
【问题描述】:

我正在学习有关 Udacity 的教程,但有些东西我不明白。下面是一个自定义类适配器:

public class WordAdapter extends ArrayAdapter<Word> {

    private static final String LOG_TAG = WordAdapter.class.getSimpleName();

    public WordAdapter(Activity context, ArrayList<Word> Word) {
        super(context, 0, Word);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        // Get the {@link AndroidFlavor} object located at this position in the list
        Word currentWord = getItem(position);

        // Find the TextView in the list_item.xml layout with the ID version_name
        TextView defaultTextView = (TextView) listItemView.findViewById(R.id.text_view_one);
        // Get the version name from the current AndroidFlavor object and
        // set this text on the name TextView
        defaultTextView.setText(currentWord.getDefaultTranslation());

        // Find the TextView in the list_item.xml layout with the ID version_number
        TextView miwokTextView = (TextView) listItemView.findViewById(R.id.text_view_lutti);
        // Get the version number from the current AndroidFlavor object and
        // set this text on the number TextView
        miwokTextView.setText(currentWord.getMiwokTranslation());

        ImageView numberImageView = (ImageView) listItemView.findViewById(R.id.image_view_id);
        numberImageView.setImageResource(currentWord.getImageResourceID());


        // Return the whole list item layout (containing 2 TextViews)
        // so that it can be shown in the ListView
        return listItemView;
    }
}

我们声明了一个名为currentWord的变量,其数据类型为Word(自定义类),然后我们在2个TextViews和1个ImageView中使用了这个变量,我们用它来获取单词。我不明白的部分是我们如何以 3 种方式而不是一种方式使用等于单个值 (getitem(position)) 的单个变量?

【问题讨论】:

  • 文本视图和图像视图不直接使用 Word。 Word 首先获取一个称为currentWord.getMiwokTranslation() 的方法,然后将一个值返回给setText()。这些方法不必返回 Word,它们可以返回任何内容。
  • 您认为getDefaultTranslation()getMiwokTranslation()getImageResourceID() 会怎样?由于Word是自定义类,请看源码。
  • @JosephYoung 啊,所以你的意思是因为 currentWord 没有返回值,所以它可以以其他方式使用?
  • 不能。它有成员和方法。下一个?

标签: java android class properties


【解决方案1】:

currentWord 引用了一个对象,这意味着它可以具有用户可以访问的各种public 方法和属性(如果您可以使用,请查看该类的源代码)。

并不是说“同一个变量”用于不同的事物,因为以下每一个都可能具有不同的值:

  • currentWord.getDefaultTranslation()(例如,这可能返回 "Dog")。
  • currentWord.getMiwokTranslation()(例如,这可能返回 "Chuku")。
  • currentWord.getImageResourceID()(例如,这可能返回 327984579294)。

这意味着每次访问一个不同的“变量”,currentWord就是用来存储这个变量集合的。

您可能想了解封装

【讨论】:

  • @hellohello 如果此答案对您有所帮助,请考虑接受。查看here 了解为什么它很重要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 2012-01-10
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 2019-08-07
  • 2014-06-16
相关资源
最近更新 更多