2

我遇到了 kotlin 委托的问题,我想将接口实现委托给一个变量。问题是超类还有一些我不想失去的其他构造函数

interface Delegate
class CustomView(myRequiredDelegate: Delegate,context:Context): ViewGroup(context), Delegate by myRequiredDelegate

现在你可以看到我在 android studio 预览中会遇到问题,因为没有构造函数(上下文:上下文)以及从 xml 初始化的其他问题,因为我必须创建

class View(myRequiredDelegate: Delegate,context: Context): ViewGroup(context), Delegate by myRequiredDelegate{
   // as you can see here I can no longer have access to call super(context, attrs...)
   // which provide more attrs for the custom view
   // and not sure if kotlin provides any other way to access the other super constructors
   constructor(context: Context):this(dummyForDelegate,context)
}

我只是想知道是否有一种方法可以在不破坏超级调用链的情况下提供委托,而不是专门针对 android 而是针对任何其他可能具有相同委托约束的情况......

4

0 回答 0