constructor methods in interfaces

后端 未结 6 2181
闹比i
闹比i 2021-02-18 21:03

Are constructor methods in interfaces bad?

6条回答
  •  旧巷少年郎
    2021-02-18 21:36

    Why do people think that anybody wants to instantiate the interface?

    What we want to do is to force implementers to implement the constructor, just like other interface methods.

    An interface is like a contract. Let's say I have an interface Queue, and I want to make sure that implementers create a constructor with one argument, which creates a singleton queue (A new queue with just that element). Why should that not be part of the contract? With at least Java interfaces, that cannot be specified.

提交回复
热议问题