簡體   English   中英

在Kotlin中使用observable時,是否需要使用newValue作為參數將其傳遞給函數?

[英]Do I need to use newValue as parameter to pass it to function when I use observable in Kotlin?

allList是私有變量,代碼A很好用。

我希望為代碼A編寫一個有趣的代碼,代碼B和代碼C之間哪個是正確的?

如果代碼B和代碼C都正確,那么代碼B和代碼C中哪一個更好?

代碼A

private var allList by Delegates.observable(listOf<MSetting>())
    { property, oldValue, newValue ->
        btnRestore.isEnabled = newValue.size >= 1
    }

代碼B

private var allList by Delegates.observable(listOf<MSetting>())
    { property, oldValue, newValue ->
        setButtonsByRecord()
    }

    private fun setButtonsByRecord(){
        btnRestore.isEnabled=allList.size>=1
    }

代碼C

private var allList by Delegates.observable(listOf<MSetting>())
    { property, oldValue, newValue ->
        setButtonsByRecord(newValue)
    }



    private fun setButtonsByRecord(my:List<MSetting>){
        btnRestore.isEnabled=my.size>=1
    }

代碼B(因為“調用此回調時屬性的值已被更改” )和代碼C都是正確的,但是只有在您需要在其他地方使用此函數的情況下,我才這樣做。 在這種情況下,B和C之間的選擇將取決於您使用的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM