Interface vs Base class

后端 未结 30 2540
甜味超标
甜味超标 2020-11-21 07:34

When should I use an interface and when should I use a base class?

Should it always be an interface if I don\'t want to actually define a base implementation of the

30条回答
  •  野的像风
    2020-11-21 07:41

    I have a rough rule-of-thumb

    Functionality: likely to be different in all parts: Interface.

    Data, and functionality, parts will be mostly the same, parts different: abstract class.

    Data, and functionality, actually working, if extended only with slight changes: ordinary (concrete) class

    Data and functionality, no changes planned: ordinary (concrete) class with final modifier.

    Data, and maybe functionality: read-only: enum members.

    This is very rough and ready and not at all strictly defined, but there is a spectrum from interfaces where everything is intended to be changed to enums where everything is fixed a bit like a read-only file.

提交回复
热议问题