C#——实现IComparable接口、IComparableT 接口、IComparer接口、IComparerT 接口和ComparerT 类DEMO
官方文檔?
IComparable 接口:https://docs.microsoft.com/zh-cn/dotnet/api/system.icomparable?view=netframework-4.8
IComparable<T> 接口:https://docs.microsoft.com/zh-cn/dotnet/api/system.icomparable-1?view=netframework-4.8
IComparer 接口:https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.icomparer?view=netframework-4.8?
IComparer<T> 接口:https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.icomparer-1?view=netframework-4.8
Comparer<T> 類:https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.comparer-1?view=netframework-4.8?
源代碼?
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Console;namespace ConsoleApp5 {public class AnimalComparer : IComparer//java compartor{public int Type { get; set; }//0-Name比較,1-Weight比較public int Compare(object x, object y)//>0: x > y{if (!(x is Animal && y is Animal))throw new ArgumentException(" ");Animal ax = x as Animal;//(Animal)x;Animal ay = (Animal)y;switch (Type){case 0:return ax.Name.CompareTo(ay.Name);//break;case 1:return ax.Weight - ay.Weight;//break;default:return 0;} }}public abstract class Animal : IComparable{protected string name;public string Name{get { return name; }set { name = value; }}public Animal(){name = "The animal with no name";}public Animal(string newName){name = newName;}public void Feed() => WriteLine($"{name} has been fed.");public override bool Equals(object obj){if (obj is Animal){Animal a = (Animal)obj;if (Name == a.Name)return true;elsereturn false;}return false;}public int Weight { get; set; }public int CompareTo(object obj)//>0:this > obj{if (obj is Animal){Animal ani = (Animal)obj;//return Name.CompareTo(ani.Name);return Weight - ani.Weight;}elsethrow new ArgumentException("錯誤參數"); }public static bool operator ==(Animal op1, Animal op2){if (op1.Name == op1.Name)return true;elsereturn false;}public static bool operator !=(Animal op1, Animal op2){if (op1.Name != op1.Name)return true;elsereturn false;}public static bool operator >(Animal op1, Animal op2){if (op1.Name.CompareTo(op1.Name) > 0)return true;elsereturn false;}public static bool operator <(Animal op1, Animal op2){if (op1.Name.CompareTo(op1.Name) < 0)return true;elsereturn false;}}public class Cow : Animal{public void Milk() => WriteLine($"{name} has been milked.");public Cow(string newName) : base(newName) { }}public class Chicken : Animal{public void LayEgg() => WriteLine($"{name} has laid an egg.");public Chicken(string newName) : base(newName) { }}class Program{static void Main(string[] args){Cow myCow = new Cow("Lea");Chicken myChicken = new Chicken("Noa");AnimalComparer ac = new AnimalComparer { Type = 0 };ArrayList animalArrayList = new ArrayList();//ArrayList默認認為其元素是object類型的animalArrayList.Add(myCow);//集合的容量能夠自動增長animalArrayList.Add(myChicken);//animalArrayList.Add(100);//裝箱//animalArrayList.Add("abc"); animalArrayList.Sort();//按名字排序animalArrayList.Sort(ac);//根據指定比較器來比較object o1 = animalArrayList[0];if (o1.GetType() == typeof(Cow))//object類里存儲了類型信息{Console.WriteLine("Animal");((Animal)o1).Feed();}if (o1 is Animal)Console.WriteLine("o1 is an animal");if (o1 is Cow)Console.WriteLine("o1 is a cow");((Animal)animalArrayList[0]).Feed();//取出數據后,默認是object類型,需轉換((Chicken)animalArrayList[1]).LayEgg();foreach (Animal item in animalArrayList) { WriteLine(item.Name); }//會執行自動轉換//(int)animalArrayList[2]//ArrayList = List<object>List<Animal> animalList = new List<Animal>();animalList.Add(myCow);//集合的容量能夠自動增長animalList.Add(myChicken);animalList[0].Feed();//取出數據后,默認是Animal類型,無需轉換((Chicken)animalList[1]).LayEgg();//但如果要作為子類對象使用,需要進行轉換foreach (Animal myAnimal in animalArrayList) { }}}}運行結果
參考文章
https://blog.csdn.net/Maybe_ch/article/details/81359408
https://blog.csdn.net/cyh1992899/article/details/52782065
https://blog.csdn.net/weixin_34274029/article/details/92958493
https://blog.csdn.net/allenjy123/article/details/7223047
https://blog.csdn.net/weixin_34189116/article/details/86345383
總結
以上是生活随笔為你收集整理的C#——实现IComparable接口、IComparableT 接口、IComparer接口、IComparerT 接口和ComparerT 类DEMO的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Visual C++——定时刷新重绘窗口
- 下一篇: C#——Ellipse(椭圆)类[继承C