Singletons, Enums and anonymous inner classes

前端 未结 3 1395
梦毁少年i
梦毁少年i 2021-01-05 00:52

As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency

3条回答
  •  轮回少年
    2021-01-05 01:08

    Add

           public String getFormattedPrice(float input) {
            return input + " €";
        }
    

    outside the overrides as the default implementation. (Next to the declaration of getPrice.) And you are good to go.

    You can also have enums implement interfaces, to define what everybody needs to implement.

提交回复
热议问题