用非公共的可访问性添加属性访问器
书中说到:
如果用属性实现接口,就必须实现匹配的get/set访问器。如果在定义接口中只包含set块。就可以给类中的属性添加get块。只有所添加的访问器的可访问修饰符比接口中定义的访问器的可访问修饰符更严格时才能这么做。因为按照定义,接口定义的访问器是公共的。也就是说,只能添加非公共的访问符。
public interface IMyInterface { int MyIntProperty { get; } } public class MyBaseClass : IMyInterface { protected int myint; public int MyIntProperty { get { return myint; } protected set { myint = value; } } }