Is there a reason you can not define the access modifier on a method or in an interface?

后端 未结 5 2003
悲哀的现实
悲哀的现实 2020-12-21 00:23

The responsibility of the visibility of a method is relegated to the class that implements the interface.

public interface IMyInterface
{
  bool GetMyInfo(st         


        
5条回答
  •  猫巷女王i
    2020-12-21 01:02

    In terms of OO - encapsulation is all about data hiding. That means whatever goes on inside a class is up to the class implementation. Which means it would be useless to contractually enforce private members.

    However, the reason one uses interfaces is because you want to ensure a class adheres to a specific contract and exposes several public members in a consistent way.

提交回复
热议问题