我最近一直盯着Kotlin,并且有Java方面的经验。在Java中,我们可以声明与父字段相同的名称。当我在Kotlin中做同样的事情时,它会给出错误name hides member of Super type User。我在这里错过了什么?
Kotlin
open class User(protected var name: String)
class TwitterUser(var name: String) : User(name)
Java的相同概念
public class A {
protected String name;
}
public class B exten
我正在Android应用程序中创建我的第一个Kotlin类。通常,出于日志记录的目的,我有一个名为TAG的常量。我在Java中要做的是: private static final String TAG = MyClass.class.getSimpleName(); 我知道在Kotlin类中,我可以使用以下方式创建TAG: private val TAG = MyClass::class.java.simpleName 这对于使用Java和Kotlin的项目来说是可以的,但是如果我启动一个只使用Kotlin的新项目呢?如何定义TAG常量?有没有更多的Kotlin方法,我没有这个奇怪的结构cl
我有一个应用程序,它使用Kotlin 1.5.0和Kapt作为mapstructs。我已将JDK更新为16,并得到以下编译错误:
Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.5.0:kapt (kapt) on project X: Compilation failure
java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x248e24b) cannot a
我有一个在Eclipse中开发的Kotlin和Spring应用程序;我记得所做的最后一个更改是在IntelliJ中更新Kotlin,现在当我尝试在Eclipse中运行我的应用程序时,控制台显示了以下错误:
No Location
EXCEPTION: java.lang.NoSuchMethodError: com.intellij.openapi.util.text.StringUtil.internEmptyString(Ljava/lang/String;)Ljava/lang/String;
at com.intellij.psi.impl.cache.TypeInfo
我在java中有一个类A,在Kotlin中有一个接口B。
// kotlin
interface B {
fun optional()
}
// java
class A implements B {
}
我想在Kotlin接口(B)中编写默认方法(Java 8特性),并想在类A中实现它,我该如何实现它?
提前谢谢。
在编写同时使用Java和Kotlin的反射代码时,开发人员应该注意哪些注意事项?
例如,我有一个使用反射的现有库,它可以很好地使用Java。但是,当我在Kotlin中使用相同的代码时,我的反射代码似乎没有获取字段上的注释。
以下是我注意到的一些不同之处。
1.获取类实例
// Example 1.1 - Java
Class<?> userClass = User.class; // From a class name
userClass = userInstance.getClass(); // OR from an instance
在Kotlin中获取
我想以一种对Kotlin调用者有用的方式扩展API,而不是Java调用者。因此,我希望鼓励Kotlin用户切换到一种新的方法,而不是困扰Java用户。我们有一个混合了Java和Kotlin的大型代码库,短期内不会是100%的Kotlin。
有没有办法添加一个@Deprecate标签或类似的标签,只在Kotlin中出现,而不是Java?
如果您对特定细节感兴趣,我们有一个接受格式化字符串的Log接口,如下所示:
Log.get().info("Log message with some $expensive $things to $format")
即使禁用了日志记录,也会在每次
在Kotlin REPL中,我试图打印一个不变的值,如下所示:
val a:Int
a = 5
print("Value of a is: ${a}")
当我用Kotlin(1.2.71)在Android (3.2)中执行上面的代码时,会引发一个错误:
错误:由于可能的重新分配,禁止捕获成员值初始化。
A=5
^
但是,当我在上运行相同部分的代码时,它成功地执行并打印输出。
<iframe src="https://pl.kotl.in/SJC7APVo7"></iframe>
如果我在Kotlin REPL中对可变的可验
在使用Java、Kotlin时,对这种情况有一些建议或答案。
Java实现
public interface IIdentified<ID> {
ID getId();
}
public class SomeEntity implements IIdentified<Long> {
private Long id;
//Some code....
@Override
public Long getId() {
return id;
}
}
我的皈依尝试。接口使用Java,类在Kotlin。我从In
我在我的kotlin项目中使用了spark 3.0.1。编译失败,出现以下错误: e: org.jetbrains.kotlin.util.KotlinFrontEndException: Exception while analyzing expression at (51,45) in /home/user/project/src/main/kotlin/ModelBuilder.kt
...
Caused by: java.lang.IllegalStateException: No parameter with index 0-0 (name=reverser$module$1 ac
我刚刚开始从Java转换到Kotlin。我有一个问题:当我思考类似于:XXClass::class.java或其他东西的时候。他们的不是在JAR中工作,而是在IDE中工作得很好。
我使用gradle :module:build或gradle :module:jar生成JAR文件。生成之后,它总是告诉我KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath。但
我在kotlin-stdlib中遇到了注释,我想知道它是用来做什么的,但不幸的是,它是没有文档的。(UPD:就在那一刻)
据我所知,将其应用于程序元素将向相应的字节码元素添加synthetic修饰符。因此,该元素在Java中变得不可见:
class MyClass {
@JvmSynthetic
fun f() { }
}
在Java代码中的某个地方:
MyClass c = new MyClass();
c.f() // Error: cannot resolve method f()
但是在Kotlin代码中仍然可以看到相同的元素:
val c = MyClass()
c.