private[this] vs private

后端 未结 9 1076
悲哀的现实
悲哀的现实 2020-11-28 01:35

In Scala I see such feature as object-private variable. From my not very rich Java background I learnt to close everything (make it private) and open (provide accessors) if

9条回答
  •  旧时难觅i
    2020-11-28 02:22

    private[this] (equivalent to protected[this]) means that that "y" is only visible to methods in the same instance. For example, you could not reference y on a second instance in an equals method, i.e., "this.y == that.y" would generate a compilation error on "that.y". (source)

    so you can do private[this] every time you want but you can have some problem if you need refer it

提交回复
热议问题