萌新柿子 2021-09-02 17:48 采纳率: 77.8%
浏览 98
已结题

一道c#练习题希望各位解答

一道c#练习题希望各位解答一下,还有如何使用icomparer这个接口进行比较枚举的值进行排序呢?
img

  • 写回答

1条回答 默认 最新

  • CSDN专家-showbo 2021-09-02 18:29
    关注

    enum可以定义数值,按照数值排下就行,
    icomparer

    img

    using 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();
            }
    
        }
    }
    
    

    怪物

    img

    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();
            }
    
        }
    }
    
    

    参考: c# 实现IComparable、IComparer接口、Comparer类的详解_匆匆那年-CSDN博客 IComparable和ICompare 接口是.net framework 中比较对象的标准方式,这两个接口之间的区别如下:1. IComparable 在要比较的对象的类中实现,可以比较该对象和另一个对象。2.IComparer 在一个单独的类中实现,可以比较任意两个对象。一般情况下,我们使用 IComparable 给出类的默认比较代码,使用其他类给出非默认的比较代码。一、I https://blog.csdn.net/lidandan2016/article/details/78480237

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月11日
  • 已采纳回答 9月3日
  • 创建了问题 9月2日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?