将对象发送到方法

时间:2015-07-28 19:29:56

标签: java android object arraylist

我编写了以下代码段但由于某种原因它不起作用:

主要

    TestClasss testClass = new TestClass;

    ArrayList<TestObject> listObj = new ArrayList<>();
    TestObject tb = new TestObject();
    tb.setName("asd");
    tb.setId("13");
    listObj.add(tb);

    testClass.addValue(listObj );

TestClasss

 public void addValalue(ArrayList<Object> list) {
        Log.e("list size is", String.valueOf(list.size()));
    }

我在以下行testClass.addValue(listObj )上有错误:

Error:(47, 11) error: method addValalue in class TestClasss cannot be applied to given types;
required: ArrayList<Object>
found: ArrayList<TestObject>
reason: actual argument ArrayList<TestObject> cannot be converted to ArrayList<Object> by method invocation conversion

我尝试创建了一个类DataObject

public class DataObject {
}

并扩展此课程TestObject

public class TestObject extends DataObject

并更改方法addValue,如下所示:

public void addValalue(ArrayList<DataObject> list) {
     Log.e("list size is", String.valueOf(list.size()));
}

但仍有相同的错误

1 个答案:

答案 0 :(得分:3)

要么改变:

public void addValalue(ArrayList<Object> list)

为:

public void addValalue(ArrayList<? extends Object> list)

BTW&#34; Valalue&#34;在字典中不是一个单词(只是确保你没有把它与'#34;值&#34混淆;)