Kotlin使我能够通过委托给主要构造函数参数来实现接口,如下所示:
class Foo(xs : ArrayList<Int>) : List<Int> by xs { }
But this exhibits the backing implementer to the user. Delegating to an anonymous also seems to be ok:
class Foo() : List<Int> by ArrayList<Int>() { }
This hides the implementation details, but we loose access to features not provided by the interface, which in this case is mutability.
因此,我希望将实现委托给不在主构造函数中的属性.我想要的和
class Foo() : List<Int> by xs {
val xs : List<Int> = ArrayList<Int>()
}
which doesn't compile.
是否有可能在类体中显式定义属性,并仍然能够将实现委托给它?