简体   繁体   English

Kotlin 带类型参数的诗人超级接口

[英]Kotlin Poet super interface with type argument

I'm trying to generate an object which implements an interface with type parameter like the following example:我正在尝试生成一个object ,它实现了一个带有类型参数的接口,如下例所示:

object HelloWorld : Feature<Intent>

I can generate the object that implements my interface like the following code:我可以生成实现我的接口的 object,如下面的代码:

val typeSpecBuilder = TypeSpec.objectBuilder("HelloWorld")
typeSpecBuilder.addSuperinterface(
      ClassName(
               "com.example.mylib",
               "Feature"
      )

How can I pass the type argument to the interface ?如何将type argument传递给interface

You can use parameterizedBy() method to use generic type.您可以使用parameterizedBy()方法来使用泛型类型。 If it is not detected by IDE, you can import manually.如果IDE没有检测到,可以手动导入。

Import plusParameter manually:手动导入plusParameter

import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter

And use like below:并使用如下:

val typeSpecBuilder = TypeSpec.objectBuilder(feature.featureName)
typeSpecBuilder.addSuperinterface(
      ClassName(
            "com.raqun.icarus.core",
            "Feature"
      ).plusParameter(ClassName("com.example.myawesomeclass", "MyAwesomeClass"))
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM