所使用的类(在Java中,第三方API,不可更改):
public class BookmarkablePageLink<T> extends Link<T> {
public <C extends Page> BookmarkablePageLink(final String id, final Class<C> pageClass)
现在我想从Kotlin中调用这个函数:
item.queue(BookmarkablePageLink("link", bookmark.page))
bookmark.page
是Java中的一个类,它的定义如下:public Class<? extends WebPage> getPage()
以下方法均无效:
item.queue(BookmarkablePageLink("link", bookmark.page))
错误: 在
constructor Bookmarkable PageLink<T : Any!, C : Page!>(...)
中,没有足够的信息可以推断参数T
item.queue(BookmarkablePageLink<>("link", bookmark.page))
item.queue(BookmarkablePageLink<Any>("link", bookmark.page))
item.queue(BookmarkablePageLink<Any, *>("link", bookmark.page))
item.queue(BookmarkablePageLink<Any, WebPage>("link", bookmark.page))
item.queue(BookmarkablePageLink<Any, in WebPage>("link", bookmark.page))
item.queue(BookmarkablePageLink<Any, out WebPage>("link", bookmark.page))
item.queue(BookmarkablePageLink<Any, T : WebPage>("link", bookmark.page))
这将是在Java中实现的“假设正确”的方法(只是意图,但不是真正的代码),但是Kotlin不支持:
item.queue(BookmarkablePageLink<Any, ? extends WebPage>("link", bookmark.page))
我最好的解决方法是这个,虽然有点丑,但却有效:
item.queue(BookmarkablePageLink<Any, WebPage>("link", bookmark.page as Class<WebPage>))
令人惊讶的是,在Java中这只需要:
item.queue(new BookmarkablePageLink<>("link", bookmark.getPage() ));
未解决的引用:T
,然后是意外的类型规范
。 - Hendy IrawangetParams
的类型是什么? - Alexey Romanovitem.queue<WebPage>(BookmarkablePageLink("link", bookmark.page))
应该可以工作。 - EpicPandaForce