[英]Kotlin interface implementation behaviour when consuming Java interface
If I have an interface in Kotlin:如果我在 Kotlin 中有一个接口:
interface KotlinInterface {
val id: String
}
I can implement it like so:我可以像这样实现它:
class MyClass : KotlinInterface {
override val id: String = "id"
}
However, if I were to consume a Java interface like this:但是,如果我要使用这样的 Java 接口:
public interface JavaInterface {
String id = "id";
}
I cannot override the id
class variable in a similar way:我不能以类似的方式覆盖id
类变量:
class MyClass : JavaInterface {
override val myId: String = JavaInterface.id //linter says: 'myId' overrides nothing
}
I also cannot make use of id
elsewhere despite it having a predefined value:尽管id
具有预定义值,但我也无法在其他地方使用它:
class MyClass : JavaInterface {
val myArray: Array<String> = arrayOf(id) // linter would say that id is not defined rather than recognising it as the string “id”
}
It appears I have to use it like so:看来我必须像这样使用它:
class MyClass {
val id: String = JavaInterface.id
val myArray: Array<String> = arrayOf(id)
}
Could anyone explain this difference in behaviour and point out anything I may be understanding wrongly?谁能解释这种行为差异并指出我可能理解错误的任何内容?
In the java interface, every variable is a static final variable and static variables can't be overridden.在java接口中,每个变量都是静态final变量,静态变量不能被覆盖。 That is why you see that lint warning.这就是为什么您会看到 lint 警告。
Edit 1:编辑1:
Kotlin interface Kotlin 接口
interface Ser {
var name: String //abstract
}
is Equivalent to java interface相当于java接口
public interface Ser {
@NotNull
String getName();
void setName(@NotNull String var1);
}
I cannot override the id class variable in a similar way:我不能以类似的方式覆盖 id 类变量:
You can't do it in Java either;你也不能用 Java 来做; the equivalent would be相当于
class MyClass implements JavaInterface {
@Override String getMyId() {
return JavaInterface.id;
}
}
getMyId
doesn't override anything. getMyId
不会覆盖任何内容。
And if you write in Java如果你用 Java 编写
class MyClass implements JavaInterface {
String id = JavaInterface.id;
}
you aren't overriding anything, because you can't override fields (in addition, MyClass.id
is a non-final instance package-private field, while JavaInterface.id
is implicitly a final static public field because interfaces don't allow any other kind).你没有覆盖任何东西,因为你不能覆盖字段(此外, MyClass.id
是一个非最终实例包私有字段,而JavaInterface.id
是隐式最终静态公共字段,因为接口不允许任何其他种类)。
I also cannot make use of id elsewhere despite it having a predefined value:尽管 id 具有预定义值,但我也无法在其他地方使用它:
Again, same as in Java;同样,与 Java 相同; you need JavaInterface.id
in both Java and Kotlin, or import some.package.JavaInterface.id
to use just id
( import static
in Java).在 Java 和 Kotlin 中都需要JavaInterface.id
,或者import some.package.JavaInterface.id
以仅使用id
(在 Java 中import static
)。
There is a difference if MyClass
implements JavaInterface
, because then in Java you can refer to JavaInterface.id
as MyClass.id
or as someInstanceOfMyClass.id
;还有,如果一个差MyClass
工具JavaInterface
,因为那时在Java中,你可以参考JavaInterface.id
作为MyClass.id
或someInstanceOfMyClass.id
; and inside MyClass
just as id
.和里面MyClass
就像id
。 But this is often considered a misfeature, so Kotlin's designers avoided it.但这通常被认为是错误的,所以 Kotlin 的设计者避免了它。
You declared constant (public static String id = "id") at Java interface.您在 Java 接口上声明了常量(public static String id = "id")。
Whereas Kotlin interface declared abstract property.而 Kotlin 接口声明了抽象属性。 See Kotlin reference: https://kotlinlang.org/docs/reference/interfaces.html请参阅 Kotlin 参考: https : //kotlinlang.org/docs/reference/interfaces.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.