Why are singleton objects more object-oriented?

后端 未结 7 2162
心在旅途
心在旅途 2020-11-29 04:38

In Programming in Scala: A Comprehensive Step-by-Step Guide, the author said:

One way in which Scala is more object-oriented than Java is that cla

7条回答
  •  日久生厌
    2020-11-29 05:14

    It's a marketing thing, really. Consider two examples:

    class foo
       static const int bar = 42;
    end class
    
    class superfoo
        Integer bar = ConstInteger.new(42);
    end class
    

    Now, what are the observable differences here?

    • in a well-behaved language, the additional storage created is the same.
    • Foo.bar and Superfoo.bar have exactly the same signatures, access, and so on.
    • Superfoo.bar may be allocated differently but that's an implementation detail

    It reminds me of the religious wars 20 years ago over whether C++ or Java were "really" Object Oriented, since after all both exposed primitive types that aren't "really" objects -- so, for example you can't inherit from int but can from Integer.

提交回复
热议问题