在Kotlin中使用泛型边界声明类属性的正确方法是什么?

时间:2018-03-02 17:12:55

标签: generics kotlin

我有这样的界面:

interface ItemClickListener<in T : Item> {
    fun onClick(item: T)
}

我的课程有点像这样:

abstract class MyClass {
    private var itemClickListener: ItemClickListener<*>? = null

    fun <I : Item> setOnClickListener(clickListener: ItemClickListener<I>) {
        this.itemClickListener = clickListener
    }
}

稍后,如果我想使用该侦听器,则会失败:

itemClickListener?.onClick(item)

我明白了:

  

Out -jected type&#39; ItemClickListener&lt; *&gt;?&#39;禁止使用公共抽象乐趣onClick(item:T):my.package.ItemClickListener&#39;

中定义的单位

在我的班级上声明itemClickListener属性的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

你只需要使MyClass通用:

abstract class MyClass<out I : Item> {
    private var itemClickListener: ItemClickListener<I>? = null

    fun setOnClickListener(clickListener: ItemClickListener<I>) {
        this.itemClickListener = clickListener
    }
}

现在itemClickListener是类型安全的,以后可以用作ItemClickListener<I: Item>

答案 1 :(得分:1)

遵循Kotlin reference星级预测:

  

对于javax.net.ssl.SSLHandshakeException: Received fatal alert: unknown_ca atsun.security.ssl.Alerts.getSSLException(Alerts.java:192) atsun.security.ssl.Alerts.getSSLException(Alerts.java:154) atsun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023) atsun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125) atsun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) atsun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) atsun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) atsun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) atsun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) atsun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546) atsun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1474) atsun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) atbeans.ApiReader.sendGet(ApiReader.java:122) atbeans.SelBeanController.showData(SelBeanController.java:43) atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) atjava.lang.reflect.Method.invoke(Method.java:498) atjavax.el.ELUtil.invokeMethod(ELUtil.java:332) atjavax.el.BeanELResolver.invoke(BeanELResolver.java:537) atjavax.el.CompositeELResolver.invoke(CompositeELResolver.java:256) atcom.sun.el.parser.AstValue.invoke(AstValue.java:283) atsom.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) atcom.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) atjavax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) atcome.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) atjavax.faces.component.UICommand.broadcast(UICommant.java:315) atjavax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) atjavax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) atcome.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) atcome.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) atcome.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) atjavax.faces.webapp.FacesServlet.service(FacesServlet.java:658) atorg.apache.cataline.core.StandardWrapper.service(StandardWrapper.java:1682) atorg.apache.cataline.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344) atorg.apache.cataline.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) atorg.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) atorg.apache.cataline.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) atorg.apache.cataline.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) atorg.apache.cataline.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316) atorg.apache.cataline.core.StandardContextValve.invoke(StandardContextValve.java:160) atorg.apache.cataline.core.StandardPipeline.doInvoke(StandardPipeline.java:734) atorg.apache.cataline.core.StandardPipeline.invoke(StandardPipeline.java:673) atcom.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) atorg.apache.cataline.core.StandardHostValve.invoke(StandardHostValve.java:174) atorg.apache.cataline.CoyoteAdapter.doService(CoyoteAdapter.java:416) atorg.apache.cataline.CoyoteAdapter.service(CoyoteAdapter.java:283) atcom.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) atcom.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) atorg.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) atorg.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) atorg.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) atorg.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) atorg.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283) atorg.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) atorg.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132) atorg.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) atorg.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) atorg.glassfish.grizzly.nio.trasport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536) atorg.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) atorg.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) atorg.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) atorg.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) atorg.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591) atorg.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571) atjava.lang.Thread.run(Thread.java:745) ,其中Foo<in T>是逆变类型参数,T相当于Foo<*>。这意味着当Foo<in Nothing>未知时,您无法以安全的方式写入Foo<*>

出现错误消息是因为T的星标是ItemClickListener<in T: Item>,无法使用任何参数调用。 This question也会提供相关信息。

解决方案是将字段ItemClickListener<in Nothing>itemClickListener的参数声明为setOnClickListener,它与类声明ItemClickListener<Item>具有相同的方差。因此,您可以使用<in Item>的任何子类型调用onClick