using System; /// <summary> /// 创建一个获取字符串数据的接口 /// </summary> /// interface IGetData { //接口定义,不使用修饰符 string GetText(); void SetText(string str); string Content { get;set;} } //创建一个获取整数的接口 interface IGetInt { int GetInt(); void SetInt(int num); int Integer { get;set;} } //接口的继承 interface IGetDataExt : IGetData { string CopySelf(); } //实现一个接口 public class Data : IGetData { //实现GetText成员函数 public string GetText() { return content; } //实现SetText成员函数 public void SetText(string str) { content = str; } //实现Content属性,指定为public public string Content { get { return content; } set { content = value; } } private string content; }; //实现两个接口 public class StringInt : IGetData, IGetInt { //实现GetText成员函数 public string GetText() { return content; } //实现SetText成员函数 public void SetText(string str) { content = str; } //实现Content属性,指定为public public string Content { get { return content; } set { content = value; } } //实现GetInt public int GetInt() { return integer; } //实现SetInt public void SetInt(int num) { integer = num; } //实现Integer属性 public int Integer { set { integer = value; } get { return integer; } } private int integer; private string content; } //实现继承的接口 public class DataExt : IGetDataExt { //实现GetText成员函数 public string GetText() { return content; } //实现SetText成员函数 public void SetText(string str) { content = str; } //实现Content属性,指定为public public string Content { get { return content; } set { content = value; } } public string CopySelf() { return content + content; } private string content; } //override接口的方法 public class VirtualData : IGetData { //定义为一个虚方法 public virtual void SetText(string str) { Console.WriteLine("VirtualData.SetText"); content = str; } //非虚方法 public string GetText() { Console.WriteLine("Virtual.GetText"); return content; } public string Content { get { return content; } set { content = value; } } private string content; } //override类实现 public class OverrideData : VirtualData { //override类的virtual方法 public override void SetText(string str) { Console.WriteLine("Override.SetText"); content = str; } //非virtual的方法 public new string GetText() { Console.WriteLine("Override.GetText"); return content; } private string content; } public class CallData { //Data的测试类 public static void test() { //测试一个接口的字符串接口 Data data = new Data(); data.SetText("SetText"); Console.WriteLine(data.GetText()); data.Content = "Content"; Console.WriteLine(data.Content); //测试两个接口的接口 StringInt si = new StringInt(); si.SetText("StringInt.SetText"); si.SetInt(100); Console.WriteLine("{0},{1}", si.GetText(), si.GetInt()); si.Content = "StrigInt.Content"; si.Integer = 200; Console.WriteLine("{0},{1}", si.GetText(), si.GetInt()); //测试继承接口 DataExt de = new DataExt(); de.SetText("DataExt.SetText"); de.CopySelf(); Console.WriteLine(de.Content); //接口的强制转化 StringInt SICast = new StringInt(); SICast.SetText("SICast.SetText"); SICast.SetInt(300); IGetData DataCast = (IGetData)SICast; Console.WriteLine(DataCast.Content); IGetInt IntCast = (IGetInt)SICast; Console.WriteLine(IntCast.Integer); //is关键字的使用 if (SICast is IGetData) Console.WriteLine("Implemented from IGetData,{0}",SICast.GetText()); else Console.WriteLine("SICast is not implemented from IGetData"); //as关键字的使用 IGetData DataAS = SICast as IGetData; if (DataAS != null) Console.WriteLine("Implemented from IGetData,{0}", SICast.GetText()); else Console.WriteLine("SICast is not implemented from IGetData"); //override与new在类中的使用 IGetData vd = new OverrideData(); //调用Override.SetText vd.SetText("VirtualData.SetText"); //调用Virtual.Get vd.GetText(); OverrideData od = new OverrideData(); //调用Override.SetText od.SetText("OverrideData.SetText"); //调用Override.GetText od.GetText(); } };