接口的属性:
请看以下代码:
C# code
public interface IMyInterface { void DoSomething(); void DoSomethingelse(); int DoThis { get; } } public class MyClass : IMyInterface { #region IMyInterface 成员 public void DoSomething() { throw new NotImplementedException(); } void IMyInterface.DoSomethingelse() { throw new NotImplementedException(); } protected int x; public int DoThis { get { return x; } set { x = value; } } #endregion } class Program { static void Main(string[] args) { MyClass m = new MyClass(); m.DoThis = 1; Console.WriteLine(m.DoThis.ToString()); Console.ReadKey(); } }


那么我就疑惑了,如果按照常理,接口定义了get访问性,基类里不应该可以set。到底怎么回事呢?
接口里的属性定义的可访性,倒不如不为空。可是为空又提示必须要有其一。
那么想必有什么作用吧?