Getting error while dealing with getter and setter in kotlin

后端 未结 2 1978
粉色の甜心
粉色の甜心 2021-01-26 05:26

I have define the data class as:

  data class chatModel(var context:Context?) {

      var chatManger:ChatManager?=null
            //getter
        get() = chat         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-26 06:03

    It is important to remember that referring to chatManger ANYWHERE in the code ends up calling getChatManger() or setChatManger(), including inside of the getter or setter itself. This means your code will end up in an infinite loop and cause a StackOverflowError.

    Read up on Properties, specifically the section about getters/setters as well as the "backing field".

提交回复
热议问题