一道c#练习题希望各位解答一下,还有如何使用icomparer这个接口进行比较枚举的值进行排序呢?
1条回答 默认 最新
- CSDN专家-showbo 2021-09-02 18:29关注
enum可以定义数值,按照数值排下就行,
icomparerusing System; using System.Linq; using System.Collections.Generic; namespace ConsoleApp1 { enum City { 北京 = 0, 上海 = 1, 广州 = 2 } class Person { public City city { get; set; } public string name { get; set; } } class SortByCity : IComparer<Person> { public int Compare(Person x, Person y) { return x.city - y.city; } } class Program { static void Main(string[] args) { var persons = new List<Person>{ new Person{name="姓名1",city=City.上海}, new Person{name="姓名2",city=City.北京}, new Person{name="姓名3",city=City.广州}, new Person{name="姓名4",city=City.北京} }; persons.Sort(new SortByCity { }); Console.Write(String.Join("\n",persons.Select(i => i.name + "," + i.city.ToString()))); Console.ReadKey(); } } }
怪物
using System; using System.Linq; using System.Collections.Generic; namespace ConsoleApp1 { enum MonsterType { 精英怪, 小怪, Boss } abstract class Monster { public string name { get; set; } public abstract MonsterType type { get; } public int Level { get; set; } public abstract void StandUp(); public abstract void Run(); public abstract void Attack(); } class Monster1 : Monster { public override MonsterType type { get => MonsterType.精英怪; } public override void StandUp() { Console.WriteLine(this.type + "站立"); } public override void Attack() { Console.WriteLine(this.type + "攻击"); } public override void Run() { Console.WriteLine(this.type + "奔跑"); } } class Monster2 : Monster { public override MonsterType type { get => MonsterType.小怪; } public override void StandUp() { Console.WriteLine(this.type + "站立"); } public override void Attack() { Console.WriteLine(this.type + "攻击"); } public override void Run() { Console.WriteLine(this.type + "奔跑"); } } class Monster3 : Monster { public override MonsterType type { get => MonsterType.Boss; } public override void StandUp() { Console.WriteLine(this.type + "站立"); } public override void Attack() { Console.WriteLine(this.type + "攻击"); } public override void Run() { Console.WriteLine(this.type + "奔跑"); } } class MonsterManger { static MonsterManger mm = null; public static MonsterManger MM { get { if (mm == null) { mm = new MonsterManger(); mm.kvMonster = new Dictionary<MonsterType, List<Monster>>(); } return mm; } } public Dictionary<MonsterType,List<Monster>> kvMonster { get; set; } public void AddMonster(Monster m) { if (!kvMonster.ContainsKey(m.type)) kvMonster[m.type] = new List<Monster>(); kvMonster[m.type].Add(m); } public void RemoveMonster(Monster m) { kvMonster[m.type].Remove(m); } public void ShowMonster() { foreach (var kp in kvMonster) { Console.WriteLine(String.Join("\n", kp.Value.Select(i => i.name + "," + i.type))); Console.WriteLine(); } } } class Program { static void Main(string[] args) { MonsterManger.MM.AddMonster(new Monster1 { name = "M1-1" }); MonsterManger.MM.AddMonster(new Monster1 { name = "M1-2" }); MonsterManger.MM.AddMonster(new Monster1 { name = "M1-3" }); MonsterManger.MM.AddMonster(new Monster2 { name = "M2-1" }); MonsterManger.MM.AddMonster(new Monster2 { name = "M2-2" }); MonsterManger.MM.AddMonster(new Monster2 { name = "M2-3" }); MonsterManger.MM.AddMonster(new Monster2 { name = "M2-4" }); MonsterManger.MM.AddMonster(new Monster2 { name = "M2-5" }); MonsterManger.MM.AddMonster(new Monster3 { name = "M3-1" }); MonsterManger.MM.ShowMonster(); Console.WriteLine("--------------"); MonsterManger.MM.RemoveMonster(MonsterManger.MM.kvMonster[MonsterType.小怪][1]);//删除M2-2小怪 MonsterManger.MM.ShowMonster(); Console.WriteLine("--------------"); MonsterManger.MM.RemoveMonster(MonsterManger.MM.kvMonster[MonsterType.小怪][1]);//删除M2-3小怪,注意下标,删除M2-2后下标会变化 MonsterManger.MM.ShowMonster(); Console.WriteLine("--------------"); MonsterManger.MM.RemoveMonster(MonsterManger.MM.kvMonster[MonsterType.精英怪][0]);//删除M1-1精英怪 MonsterManger.MM.ShowMonster(); Console.ReadKey(); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 乌班图ip地址配置及远程SSH
- ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
- ¥15 PSPICE制作一个加法器
- ¥15 javaweb项目无法正常跳转
- ¥15 VMBox虚拟机无法访问
- ¥15 skd显示找不到头文件
- ¥15 机器视觉中图片中长度与真实长度的关系
- ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
- ¥15 java 的protected权限 ,问题在注释里
- ¥15 这个是哪里有问题啊?