Tag: 科特林

Kotlin构造函数中的语句

有没有办法在Kotlin中混合语句(如打印语句)和成员分配? 这里是我想要做的一个例子(用Java): class MySystem { ComponentA componentA; ComponentB componentB; public MySystem() { System.out.println(“Initializing components”); this.componentA = new ComponentA(); System.out.println(“Constructed componentA”); this.componentB = new ComponentB(); System.out.println(“Constructed componentB”); } } 感谢任何输入,谢谢。

Kotlin文档是否正确?

代码(下面显示)是否正确? 这是来自Kotlin-docs.pdf的第63页,这也是https://kotlinlang.org/docs/reference/generics.html的最后一个代码片段 fun cloneWhenGreater(list: List, threshold: T): List where T : Comparable, T : Cloneable { return list.filter { it > threshold }.map { it.clone() } } 按照原样,编译器失败:1. 在kotlin中定义的接口Comparable的预期types参数 2. types推断失败。 期望的types不匹配:推断的types是列表,但期望列表 3. 不能访问“克隆”:它在“克隆” 前两个错误很容易通过将代码更改为以下来解决: fun cloneWhenGreater(list: List, threshold: T): List where T : Comparable, T : Cloneable { return list.filter { it > threshold […]

Kotlin静态function:伴随对象,@JvmStatic @JvmField

我刚刚开始使用Kotlin Programming Language ,它比Java更酷。 我对静态方法和领域有一些疑问, Q1:官方文件说 如果您将这些函数注释为@JvmStatic,Kotlin也可以为命名对象或伴随对象中定义的函数生成静态方法。 但是,如果你看到下面我可以访问bar()方法作为静态方法,它不使用@JvmStatic注释工作。 但官方文档抛出错误 – > Kotlin静态方法 。 Class C{ companion object{ @JvmStatic fun foo() { } fun bar(); } } fun main(args: Array) { C.foo(); C.bar(); //this line works fine } Q2:我真的需要@JvmStatic和@JvmField来使事情变得静态吗? :正如你可以看到的companion对象,事情按预期工作。 happyCoding

在Kotlin中创建一个注释实例

我有一个用Java编写的框架,使用reflection,获取注释上的字段,并根据它们做出一些决定。 在某些时候,我也可以创建一个注释的临时实例并自己设置字段。 这部分看起来像这样: public @interface ThirdPartyAnnotation{ String foo(); } class MyApp{ ThirdPartyAnnotation getInstanceOfAnnotation(final String foo) { ThirdPartyAnnotation annotation = new ThirdPartyAnnotation() { @Override public String foo() { return foo; } }; return annotation; } } 现在我正在努力做Kotlin的确切的事情。 请记住,注释位于第三方jar中。 无论如何,这里是我在Kotlin试试的: class MyApp{ fun getAnnotationInstance(fooString:String):ThirdPartyAnnotation{ return ThirdPartyAnnotation(){ override fun foo=fooString } } 但编译器抱怨:Annotation类不能被实例化 所以问题是:我应该如何在Kotlin中做到这一点?

Kotlin Spring启动@ConfigurationProperties列表

我想用Kotlin读取yaml配置文件,下面是我的代码: application.yml message: messages: – name: abc type: aaa size: 10 – name: xyz type: bbb size: 20 MessageConfig.kt package com.example.demokotlin import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.context.annotation.Configuration import java.math.BigDecimal @ConfigurationProperties(prefix = “message”) @Configuration class MessageConfig { lateinit var messages: List } class Message { lateinit var name: String lateinit var type: String lateinit var size: BigDecimal } […]

Kotlin插件崩溃Android Studio

我已经更新了Kotlin插件到版本1.1.61,但Android工作室不启动新的插件。 错误: Plugin ‘org.jetbrains.kotlin’ failed to initialize and will be disabled. Please restart Android Studio. com.intellij.openapi.extensions.impl.PicoPluginExtensionInitializationException: Duplicate registration for EP: org.jetbrains.uast.uastLanguagePlugin: original plugin com.intellij, new plugin org.jetbrains.kotlin

Kotlin错误:无法findorg.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7

我将Kotlin插件安装到我的应用程序中(v1.1.1-release-Studio2.2-1),然后选择“在项目中配置Kotlin”我选择了编译器和运行时版本1.0.7。 Kotlin更新了我的Gradle文件。 现在,当我尝试建立我得到: 错误:配置项目“:app”时发生问题。 无法解析配置的所有依赖关系:“app:_debugApkCopy”。 找不到org.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7。 要求: 所有MyApplication:应用程序:未指定 我不知道我在这里错过了什么。

在浏览器中运行Kotlin HTML Builder

我是Kotlin的新开发人员。 我喜欢这门语言,而且我喜欢用它来完成Web应用程序的轻松。 问题是,我不知道如何在浏览器中运行Kotlin HTML builder文件,以便我可以在kotlin中创建一个基本的网页框架。 我可以在IDE中输出它,但是看起来好像是让它在我的浏览器中运行是很愚蠢的。 这可能是一个愚蠢的问题,我错过了一些非常明显的东西,但我似乎无法在网上find答案。 请记住,我没有使用Intelli-J IDE。 会喜欢,但不能付出鼻子只是为了在Kotlin做网页开发。 一直在使用Eclipse。 提前致谢。

Kotlin无法访问受保护的抽象方法

我有以下的类结构: abstract class Abstr{ protected abstract fun m() } class Child : Abstr(){ private val subChild: Abstr = Child() override fun m() = subChild.m()// Error:(12, 18) Kotlin: Cannot access ‘m’: it is protected in ‘Abstr’ } 我遇到了一个exceptionKotlin: Cannot access ‘m’: it is protected in ‘Abstr’ 这有点混乱,因为相同的结构是合法的Java。 根据kotlin文档 protected – 仅在此类中可见+在子类中也可见; 它是错误还是预期的行为?

没有使用对象的Kotlin lambda

我正试图从java转换到kotlin。 目前的java界面是这样的: interface MyInterface { void foo(int x, int y); } MyInterface testing = (int a, int b) -> System.out.print(“TESTING”); 我目前的kotlin转换是: interface MyInterface { fun foo(x:Int, y:Int) } val kotlinConversion = object: MyInterface { override fun foo(x: Int, y: Int) { println(“TESTING”) } } 有没有办法写variableskotlinConversion ,使其与java中的类似,而不必重写函数?