mhy_0210 2021-05-29 22:13 采纳率: 0%
浏览 46

C#下标为字符串类型的索引器重载

补全程序

测试输入: 红楼梦 西游记 三国演义 水浒传 43 29 39 27 红楼梦 预期输出: 43

using System;
namespace IndexSample
{
    class Book
    {
        string id;
        int price;
        public string ID
        {
            get
            { return id; }
            set
            { id = value; }
        }
        public int Price
        {
            get
            { return price; }
            set
            { price = value; }
        }
        public Book()
        {
            id = ""; price = 0;
        }
    }
    class MyClass
    {
        private Book[] data;
        public int Length
        {
            get
            { return data.Length; }
        }
        // 索引器定义,根据下标访问data
        public Book this[int index]
        {
            get
            {
                if (index >= 0 && index <= Length - 1)
                    return data[index];
                else
                    return null;
            }
            set
            {
                if (index >= 0 && index <= Length - 1)
                    data[index] = value;
            }
        }
        // 重载下标为字符串类型的索引器
        public Book this[string index]
        {
            get
            {
                //************BEGIN****************
                

                //*************END*****************
            }
        }
        public MyClass(Book[] st1)
        {
            data = st1;
        }
        public MyClass(string[] id1, int[] price1)
        {
            data = new Book[id1.Length];
            for (int i = 0; i < id1.Length; i++)
            {
                data[i] = new Book();
                (data[i]).ID = id1[i]; ;
                (data[i]).Price = price1[i];
            }
        }
    }
    class MyClient
    {
        static void Main(string[] args)
        {
            //将测试集输入的第一行中的每个单词组成字符串数组
            string[] str = (Console.ReadLine()).Split(' ');
            //将测试集输入的第一行中的每个字符串组成价格数组
            string[] prices = (Console.ReadLine()).Split(' ');
            int[] price = new int[prices.Length];
            for (int i = 0; i < price.Length; i++)
            {
                price[i] = Convert.ToInt32(prices[i]);
            }
            MyClass mc = new MyClass(str, price);
            //提取第三行-要查看价格的书名
            string book = Console.ReadLine();
            // 输出此书的价格
            Console.WriteLine("The price of {0} is {1}", mc[book].ID, mc[book].Price);
        }
    }
}

  • 写回答

1条回答 默认 最新

  • 红薯不甜 全栈领域新星创作者 2024-05-24 15:37
    关注
    
    using System;
    using System.Collections.Generic;
    
    class BookCollection
    {
        private Dictionary<string, int> books;
    
        public BookCollection()
        {
            books = new Dictionary<string, int>();
        }
    
        public void AddBook(string name, int pageCount)
        {
            books[name] = pageCount;
        }
    
        // 重载索引器
        public int this[string name]
        {
            get
            {
                if (books.ContainsKey(name))
                {
                    return books[name];
                }
                else
                {
                    throw new KeyNotFoundException($"书籍 {name} 未找到。");
                }
            }
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            BookCollection collection = new BookCollection();
            collection.AddBook("红楼梦", 43);
            collection.AddBook("西游记", 29);
            collection.AddBook("三国演义", 39);
            collection.AddBook("水浒传", 27);
            
            Console.WriteLine(collection["红楼梦"]);  // 预期输出:43
        }
    }
    

    BookCollection类有一个用于存储书名和页数的Dictionary(名为books)。类中还有一个添加书籍的方法 AddBook,它接受书名和页数作为参数,并将它们添加到字典中。重载的索引器接受一个字符串(书名),如果书名存在于字典中,则返回相应的页数。如果书名不存在,则抛出一个KeyNotFoundException异常。这样,你就可以通过书名来获取书籍的页数了。

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵