gpt4 book ai didi

properties - Kotlin 吸气二传手没有领域

转载 作者:行者123 更新时间:2023-12-02 13:14:26 25 4
gpt4 key购买 nike

我想创建一个没有字段的getter和setter。目前,我具有此功能:

/** Only the color component will be considered, alpha has no effect */ 
fun setCircleColor(@ColorInt color : Int){
circlePaint.color = ColorUtils.setAlphaComponent(color, circlePaint.alpha)
}

理想情况下,我想要这样的东西
circleColor: @ColorInt Int 
get(){
return circlePaint.color
set(value){
circlePaint.color = ColorUtils.setAlphaComponent(color, circlePaint.alpha)
}

如您所见,我不想将circleColor存储在var中,但是仍然可以调用
myView.color = 0xFFaaaaaa而不是 myView.setColor(0xFFaaaaaa)。这可能吗?

最佳答案

我建议按照您打算的方式实施它:

var circleColor: Int
@ColorInt get() = circlePaint.color
set(@ColorInt color) {
circlePaint.color = ColorUtils.setAlphaComponent(color, circlePaint.alpha)
}

关于properties - Kotlin 吸气二传手没有领域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com