Interface &引用;“接口中的代码”;Kotlin,他们如何避免“这一点?”;致命的死亡钻石;?
我在读这篇文章,它说你们可以用Kotlin接口编写代码。Java不允许在接口中编写代码来避免菱形问题。如果Kotlin允许在接口中编写代码,并且可以在一个类中实现多个接口,这难道不会再次造成“钻石问题”吗?场景1 两个接口都有具有相同签名的方法,并且在接口中都没有实现,因此需要实现具有相同签名的单个方法 范例Interface &引用;“接口中的代码”;Kotlin,他们如何避免“这一点?”;致命的死亡钻石;?,interface,kotlin,diamond-problem,Interface,Kotlin,Diamond Problem,我在读这篇文章,它说你们可以用Kotlin接口编写代码。Java不允许在接口中编写代码来避免菱形问题。如果Kotlin允许在接口中编写代码,并且可以在一个类中实现多个接口,这难道不会再次造成“钻石问题”吗?场景1 两个接口都有具有相同签名的方法,并且在接口中都没有实现,因此需要实现具有相同签名的单个方法 范例 interface InterfaceA { fun sum(a: Int, b: Int) } interface InterfaceB { fun sum(x: In
interface InterfaceA {
fun sum(a: Int, b: Int)
}
interface InterfaceB {
fun sum(x: Int, y: Int)
}
class TestClass : InterfaceA, InterfaceB {
override fun sum(x: Int, y: Int) {
return a+b
}
}
interface InterfaceA {
fun sum(a: Int, b: Int):Int = a+b
}
interface InterfaceB {
fun sum(x: Int, y: Int)
}
class TestClass : InterfaceA, InterfaceB {
override fun sum(x: Int, y: Int) {
return a+b
}
}
interface InterfaceA {
fun sum(a: Int, b: Int): Int {
print("InterFaceA");
return a + b
}
}
interface InterfaceB:InterfaceA {
override fun sum(a: Int, b: Int): Int {
print("InterFaceB");
return a + b
}
}
interface InterfaceC:InterfaceA {
override fun sum(a: Int, b: Int): Int {
print("InterFaceC");
return a + b
}
}
interface InterfaceD : InterfaceB, InterfaceC {
override fun sum(a: Int, b: Int): Int {
print("InterFaceD");
return a + b
}
}
情景2
两个接口具有相同签名的方法,在这种情况下,不同的返回类型将是错误的
范例
interface InterfaceA {
fun sum(a: Int, b: Int)
}
interface InterfaceB {
fun sum(x: Int, y: Int)
}
class TestClass : InterfaceA, InterfaceB {
override fun sum(x: Int, y: Int) {
return a+b
}
}
interface InterfaceA {
fun sum(a: Int, b: Int):Int = a+b
}
interface InterfaceB {
fun sum(x: Int, y: Int)
}
class TestClass : InterfaceA, InterfaceB {
override fun sum(x: Int, y: Int) {
return a+b
}
}
interface InterfaceA {
fun sum(a: Int, b: Int): Int {
print("InterFaceA");
return a + b
}
}
interface InterfaceB:InterfaceA {
override fun sum(a: Int, b: Int): Int {
print("InterFaceB");
return a + b
}
}
interface InterfaceC:InterfaceA {
override fun sum(a: Int, b: Int): Int {
print("InterFaceC");
return a + b
}
}
interface InterfaceD : InterfaceB, InterfaceC {
override fun sum(a: Int, b: Int): Int {
print("InterFaceD");
return a + b
}
}
在本例中,编译器显示错误,因为两个方法必须具有相同的返回类型
diamond问题与Kotlin和Java中不允许的类的多重继承相关,尽管您可以通过实现具有两个接口的接口来创建diamond shape场景,但在Kotlin中,您需要覆盖所有方法,否则这是编译时错误,从而避免了diamond shape问题
范例
interface InterfaceA {
fun sum(a: Int, b: Int)
}
interface InterfaceB {
fun sum(x: Int, y: Int)
}
class TestClass : InterfaceA, InterfaceB {
override fun sum(x: Int, y: Int) {
return a+b
}
}
interface InterfaceA {
fun sum(a: Int, b: Int):Int = a+b
}
interface InterfaceB {
fun sum(x: Int, y: Int)
}
class TestClass : InterfaceA, InterfaceB {
override fun sum(x: Int, y: Int) {
return a+b
}
}
interface InterfaceA {
fun sum(a: Int, b: Int): Int {
print("InterFaceA");
return a + b
}
}
interface InterfaceB:InterfaceA {
override fun sum(a: Int, b: Int): Int {
print("InterFaceB");
return a + b
}
}
interface InterfaceC:InterfaceA {
override fun sum(a: Int, b: Int): Int {
print("InterFaceC");
return a + b
}
}
interface InterfaceD : InterfaceB, InterfaceC {
override fun sum(a: Int, b: Int): Int {
print("InterFaceD");
return a + b
}
}
重写是必要的,否则编译器将显示错误,并且不会继续进行。
Java不允许在接口中编写代码。
它允许:是的,我说过它不允许,现在它允许(在Java 8之后)。请阅读我链接的文章。如果您试图创建一个示例,看看会发生什么?java和kotlin都不支持多重继承。你真的明白“钻石问题”指的是什么吗?@MarcinOrlowski他指的是一个实现多个接口的类,即使接口中有代码。