制作网页类型一般分为什么,沈阳做网站seo,做软件找什么公司,163 com免费邮箱注册接口 接口可以看作是多态的一种。它打破了里氏替换原则。即不是共同的生物#xff0c;比如动物#xff08;狗#xff09;和人#xff08;老师#xff09;#xff0c;这两个类 却都有吃这种方法。但是继承里不能把老师和狗归为一个类。所以这时用接口来解决这种问题。 语法…接口 接口可以看作是多态的一种。它打破了里氏替换原则。即不是共同的生物比如动物狗和人老师这两个类 却都有吃这种方法。但是继承里不能把老师和狗归为一个类。所以这时用接口来解决这种问题。 语法
interface IStand
{void Eat();
}
注意 1命名I开头默认省略修饰符abstractoverride 2接口可以被实现多个打破了单根性 3接口不是类却类似父类的存在 4接口还可以作为参数 与多态的同 1强制性 2提取多个子类的共同方法作为多态的优化。
可以用微软提供的接口进行快速的操作来实现功能。 比如排序。
IComparable 接口的CompareTo方法
class Pc : IComparable
{public string i;public Pc(string i){this.i i;}public int CompareTo(object obj){return i.CompareTo((obj as Pc).i);//或是升序比较反过来或是降序。}
}static void Main(string[] args)ListPc pp new ListPc();pp.AddRange(new Pc[] {new Pc(2),new Pc(1),new Pc(3)});pp.Sort();foreach (Pc item in pp){Console.WriteLine(item.i);}}两个对象直接比较 IComparer 的Compare 方法
class compare
{private string name;private int age;public compare() { }public compare(int age){this.age age;}public string Name { get name; set name value; }public int Age { get age; set age value; }}class testCompare : IComparercompare
{public int Compare(compare x, compare y){return x.Age.CompareTo(y.Age);}
}static void Main(string[] args){Listcompare cp new Listcompare();cp.AddRange(new compare[] {new compare(22),new compare(18),new compare(20)});cp.Sort(new testCompare()); //这里有区别。foreach (var item in cp){Console.WriteLine(item.Age);}}