gpt4 book ai didi

java - 关于包装我的可变参数参数的困惑

转载 作者:行者123 更新时间:2023-11-30 02:19:35 26 4
gpt4 key购买 nike

当编译我的类(它是我的 ActiveAndroid ORM 数据库中的一个表)时,出现此错误:

警告:(33, 50) 可变参数方法的非可变参数调用,最后一个参数的参数类型不准确;转换为对象以进行可变参数调用转换为 Object[] 以进行非可变参数调用并抑制此警告

这里是this引用的方法:

public static ChildMedicine getChildMedicine(Child child, Medicine medicine)
{
return new Select()
.from(ChildMedicine.class)
.where("Child = ? AND Medicine = ?", new Long[]{child.getId(), medicine.getId()})
.executeSingle();
}

我想从我的 ActiveAndroid ORM 数据库表中返回所有 ChildMedicine 对象,其中 Child 列等于传入子参数的 Long Id,而 Medicine 列等于传入 medicine 参数的 Long Id。

我被建议用这样的显式数组创建来包装 wrap vararg 参数:

public static ChildMedicine getChildMedicine(Child child, Medicine medicine)
{
return new Select()
.from(ChildMedicine.class)
.where("Child = ? AND Medicine = ?", new Object[]{new Long[]{child.getId(), medicine.getId()}})
.executeSingle();
}

但是,这会不会导致我的 Select() 方法无法正常工作,因为我现在在该方法的 where 部分中有一个包含两个 Long 数组的数组,而不是一个包含两个 Long 的 Long 数组?

我有点糊涂了!

非常感谢任何帮助...

谢谢,山姆

最佳答案

如果不知道 where 方法的签名,我无法确定,但看来您需要的是:

return new Select()
.from(ChildMedicine.class)
.where("Child = ? AND Medicine = ?", new Object[]{child.getId(), medicine.getId()})
.executeSingle();

这是假设 where 的签名是 where (String str, Object... params)

另一个应该有效的选项:

return new Select()
.from(ChildMedicine.class)
.where("Child = ? AND Medicine = ?", child.getId(), medicine.getId())
.executeSingle();

I am suggested to wrap the wrap vararg arguments with explicit array creation

不,这不是你被建议的。建议您将 ObjectObject[] 传递给 where,具体取决于您是否希望使用 Long[] 被视为单个参数或多个参数。传递包含单个 Long[] 元素的 Object[] 没有任何意义。

关于java - 关于包装我的可变参数参数的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28798223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com