小编Pra*_*arg的帖子

无法将自定义 getter 与委托属性一起使用

我是 kotlin 新手,无法弄清楚这个问题。

我有一个不可为 null 的属性,可能会也可能不会使用。因此,我以一种惰性的方式委托它,以便在需要时对其进行初始化。

private val labelAnimator: ObjectAnimator by lazy {
    ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
}
Run Code Online (Sandbox Code Playgroud)

但是,我还需要在每次访问该对象时设置该对象的一些属性。

fun getLabelAnimator(): ObjectAnimator {
    labelAnimator.duration = (if (isFloatingLabelAnimating) 300 else 0).toLong()
    return labelAnimator
}
Run Code Online (Sandbox Code Playgroud)

但我无法使用自定义 getter,因为该属性是委托的。我如何以最好的方式实现这一目标?

getter properties kotlin delegated-properties

3
推荐指数
1
解决办法
1994
查看次数

标签 统计

delegated-properties ×1

getter ×1

kotlin ×1

properties ×1