The purpose of interfaces continued

后端 未结 13 1437
轮回少年
轮回少年 2020-12-03 02:28

OK so I gather that Interfaces are a way to enforce that an object implements a certain amount of functionality, without having to use inheritance. Kind of like a contract.

13条回答
  •  清歌不尽
    2020-12-03 02:44

    and it has no implementation as such then whoever uses your interface has to write it from scratch..every time.

    Each implementation of the interface can be different. The point is that you can use interface without knowing the implementation. Consider example:

    public interface ILogger
    {
        void WriteMessage(string message);
    }
    

    Your application may use ILogger interface for logging errors/debug information, etc. But it doesn't matter how logger is implemented - it can be FileSystemLogger, or DatabaseLogger, or any other implementation. So you will be able to substitute implementations at any time without changing all places in code where logging was mentioned.

提交回复
热议问题