What is the difference between textView.setText(string) and textView.text = $string

前端 未结 5 1760
情歌与酒
情歌与酒 2021-01-20 03:46

Hi I am making a app with Kotlin and I found that I can both use

textView.setText(str)

and

textView.text =         


        
5条回答
  •  耶瑟儿~
    2021-01-20 04:44

    The method setText() and getText() are called setters and getters, they are automatically generated in kotlin.

    class ClassName{
        var name: String= "some_value"
    }
    

    You can use the name property directly with the object of the class or you can also use the auto-generated setter method.

    class Another{
        var c = ClassName()
    
        c.name = "value"
        c.setName("value")
    }
    

    But if a property starts with a val instead of var then it is immutable and does not allow a setter.
    In case you want to read further:- Setters and getters in kotlin

提交回复
热议问题