小编Ben*_*rth的帖子

在Kotlin中创建接口时,是否已获取/设置属性是否重要?

在Kotlin接口中,使用空的get/set语句声明属性是否重要?

例如...

interface ExampleInterface {
    // These...
    val a: String
        get
    var b: String
        get
        set

    // ...compared to these...
    val c: String
    var d: String
}
Run Code Online (Sandbox Code Playgroud)

我很难注意到差异.

在实现接口时,如果我对属性使用getter/setter,或者直接设置值,似乎并不重要.

当通过java访问这些时,val它们都有getter,而且var它们都有getter和setter.

public void javaMethod(ExampleInterface e) {
    e.getA();

    e.getB();
    e.setB();

    e.getC();

    e.getD();
    e.setD();
}
Run Code Online (Sandbox Code Playgroud)

properties getter-setter kotlin

8
推荐指数
1
解决办法
2960
查看次数

Gradle compileKotlin includeRuntime不向jar添加运行时

我有一个Kotlin Gradle项目,我想将Kotlin的运行时和stdlib包含在jar文件中.我目前正在使用它,但是当我使用build.gradle配置构建项目时,它不包括运行时或stdlib.

compileKotlin {
    kotlinOptions {
        includeRuntime = true
        noStdlib = false
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我用来在jar中包含运行时/ stdlib的Gradle代码,但它不像我期望的那样工作.以下是某些上下文的完整build.gradle文件:https: //github.com/BenWoodworth/CrossPlatformGreeter/blob/bd1da79f36e70e3d88ed871bc35502ecc3a852fb/build.gradle#L35-L43

Kotlin的Gradle文档似乎表明将kotlinOptions.includeRuntime设置为true应该在生成的.jar中包含Kotlin运行时.
https://kotlinlang.org/docs/reference/using-gradle.html#attributes-specific-for-kotlin

编辑: 这可能是相关的.当我运行compileKotlin时,我收到了一些与运行时相关的警告:

:compileKotlin
w: Classpath entry points to a non-existent location: <no_path>\lib\kotlin-runtime.jar

BUILD SUCCESSFUL
Run Code Online (Sandbox Code Playgroud)

runtime jar std gradle kotlin

5
推荐指数
1
解决办法
1305
查看次数

标签 统计

kotlin ×2

getter-setter ×1

gradle ×1

jar ×1

properties ×1

runtime ×1

std ×1