你如何解决 Kotlin 中的循环导入

How do you resolve circular imports in Kotlin

我是 Kotlin 编程的新手,我已经设法 运行 解决了 classic 循环依赖问题 - 我知道 Kotlin 可以解决这些问题,但我想知道我将如何改变我的设计来避免它。我应该在下面使用什么结构或 Kotlin 功能?

import MyClass

interface MyInterface {
    fun useMyClass(myInstance: MyClass)
}
import MyInterface

class MyClass(myList: List<MyInterface>) {
    val storedList: List<MyInterface> = myList
    var myValue: Int = 10
}

我想 MyClass 存储实现 MyInterface 的多个对象,但我也希望每个对象都引用它们已传递给的 class,即每个useMyClass 的调用将具有 useMyClass(this).

的签名

例如,我可以创建一个 class

class ImplementingMyInterfaceClass(): MyInterface {
    override fun useMyClass(myInstance: MyClass) {
         myInstance.myValue += 10
    }
}

并在 MyClass 中的某处调用它:

ImplementingMyInterfaceClass().useMyClass(this)

从技术上讲,我可以在中间创建另一个构造,MyInterface 将使用它,MyClass 将使用 inherited/implemented,但这感觉不正确。有什么建议吗?

注意:在我的具体问题中,将 MyInterface 的每个实现视为一种 "modifier" 可能会有所帮助(因为它将修改 class 的实例) - MyClass 个实例应该知道它的修饰符并且每个修饰符应该能够修改那个实例。

这在很大程度上取决于接口必须做什么,但您可以将其函数参数限制为 MyClass 实现的某个接口:

interface MyInterface {
    fun increaseSomeValue(someValueHolder: MySubInterface)

    interface MySubInterface {
        var myValue: Int
    }
}

class MyClass(myList: List<MyInterface>): MyInterface.MySubInterface {
    val storedList: List<myInterface> = myList
    override var myValue: Int = 10
}

或者您的界面可以采用 属性 参数:

interface MyInterface {
    fun increaseSomeValue(someValue: KMutableProperty<Int>)
}

class MyInterfaceImpl: MyInterface {
    override fun increaseSomeValue(someValue: KMutableProperty<Int>) {
        someValue.setter.call(someValue.getter.call() + 10)
    }
}

// from MyClass:
storedList.first().printSomeValue(::myValue)

在我们不需要同时获取和设置的其他情况下,采用更通用的函数参数(可以传递 lambda)可能更简洁:

interface MyInterface {
    fun printSomeValue(valueProvider: () -> Int)
}

class MyInterfaceImpl: MyInterface {
    override fun printSomeValue(valueProvider: () -> Int) {
        println(valueProvider())
    }
}

// from MyClass:
storedList.first().printSomeValue(::myValue)
// or
storedList.first().printSomeValue { 1..10.random() }