我如何使用嵌套接口来修复此错误

问题描述 投票:0回答:1

在您可以看到嵌套接口的代码中。 Intellij Idea中的错误是:

Kotlin:以下候选人都不适用::25

错误在行中:返回整数(this.value + other.value),并且“ +”符号出现在红色中。

我真的很感谢任何帮助解决问题的帮助。

谢谢你

interface T1 interface Semigroup<T1> { fun builder(x: T1): SemigroupElement<T1> interface SemigroupElement<T1> { fun multiply(other: SemigroupElement<T1>): SemigroupElement<T1> val value: T1 } } fun main() { class Z : Semigroup<Int> { override fun builder(x: Int): Semigroup.SemigroupElement<Int> = Integer(x) inner class Integer<Int>(val x: Int) : Semigroup.SemigroupElement<Int> { override val value = x override fun multiply(other: Semigroup.SemigroupElement<Int>): Semigroup.SemigroupElement<Int> { return Integer(this.value + other.value) } } } }

在您可以看到嵌套接口的代码中。 Intellij Idea中的错误是:

Kotlin:以下候选人都不适用::25
错误在行中:返回整数(this.value + other.value),并且“ +”符号出现在红色中。

我真的很感谢任何帮助解决问题的帮助。

谢谢你

您将内部类宣布为具有名为

Integer

的类型参数。 内部类中的每一次the都引用了此类型参数,而不是内置

Int

类型。如果我们只是将这种类型的参数to

Int

.

kotlin inner-classes
1个回答
0
投票

T
inner class Integer<T>(val x: T) : Semigroup.SemigroupElement<T> {
    override val value = x

    override fun multiply(other: Semigroup.SemigroupElement<T>): Semigroup.SemigroupElement<T> {
        return Integer(this.value + other.value)
    }
}
既有类型
this.value
,显然无法将其添加在一起。
您显然是要使用
other.value,并且不应该有任何类型的参数。 T
side Note:
kotlin.Int

是多余的。如果您认为这是宣布
Integer
的必要条件,那么您是错误的。


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.