What replaces class variables in scala?

后端 未结 2 2064
长发绾君心
长发绾君心 2020-12-13 17:57

In Java I sometimes use class variables to assign a unique ID to each new instance. I do something like

public class         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 18:03

    To amplify on Thomas' answer:

    The object definition is usually put in the same file with the class, and must have the same name. This results in a single instance of an object having the name of the class, which contains whatever fields you define for it.

    A handy do-it-yourself Singleton construction kit, in other words.

    At the JVM level, the object definition actually results in the definition of a new class; I think it's the same name with a $ appended, e.g. Foo$. Just in case you have to interoperate some of this stuff with Java.

提交回复
热议问题