温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中集合Collections购物车Shopping Cart的示例分析

发布时间:2021-08-17 10:44:14 来源:亿速云 阅读:175 作者:小新 栏目:编程语言

这篇文章主要介绍了C#中集合Collections购物车Shopping Cart的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

这篇是对象与集合操练,物件的创建,集合的一些基本功能,如添加,编辑,删除等功能。

对象,即是网店的商品物件,Insus.NET只为其添加2个属性,物件的ID的Key和名称ItemName以及2个构造函数,最后一个方法是重写ToString()方法。

C#中集合Collections购物车Shopping Cart的示例分析

class Item  {   private int _key;   public int Key   {    get    {     return _key;    }    set    {     _key = value;    }   }   private string _ItemName;   public string ItemName   {    get { return _ItemName; }    set { _ItemName = value; }   }   public Item()   {   }   public Item(int key, string itemName)   {    this._key = key;    this._ItemName = itemName;   }   public override string ToString()   {    return string.Format("ID: {0}; Name: {1}。",_key,_ItemName);   }  }

有了物件,你可以创建你的购物车Shopping Cart:

C#中集合Collections购物车Shopping Cart的示例分析

class ShoppingCart  {   private SortedList<int, Item> _sl = new SortedList<int, Item>();   public void Add(Item item) //物件添加   {    this._sl.Add(item.Key, item);   }   public void Edit(Item item) //编辑物件   {    if (this._sl.ContainsKey(item.Key))    {     this._sl[item.Key] = item;    }   }   public void Delete(Item item) //删除物件   {    this._sl.Remove(item.Key);   }   public Item this[int key] //索引器   {    get    {     if (!this._sl.ContainsKey(key))     {      return null;     }     else     {      return this._sl[key];     }    }   }   public virtual int Count //集合中物件数量   {    get    {     return this._sl.Count;    }   }   public virtual IEnumerable<Item> Items //获取所有物件   {    get    {     return this._sl.Values;    }   }  }

下面是在控制台测试上面写好的集合购物车:


C#中集合Collections购物车Shopping Cart的示例分析

class Program  {   static void Main(string[] args)   {    ShoppingCart sc = new ShoppingCart();    var item1 = new Collections.Item();    item1.Key = 1;    item1.ItemName = "Huawei V8";    sc.Add(item1);    var item2 = new Collections.Item();    item2.Key = 2;    item2.ItemName = "Huawei V9";    sc.Add(item2);    var item3 = new Collections.Item();    item3.Key = 3;    item3.ItemName = "Huawei V10";    sc.Add(item3);    Console.WriteLine("使用索引器,输出对象:");    Console.WriteLine(sc[3].ToString());    Console.WriteLine("集合中对象数量:");    Console.WriteLine(sc.Count);    Console.WriteLine("列出所有对象:");    sc.Items.ForEach(delegate (Collections.Item item)    {     Console.WriteLine(item.ToString());    });   }  }

按Ctrl + F5输出结果:

C#中集合Collections购物车Shopping Cart的示例分析

最后演示编辑Edit和删除Delete的功能:

C#中集合Collections购物车Shopping Cart的示例分析

var item4 = new Collections.Item();    item4.Key = 2;    item4.ItemName = "Huawei Mate10";    sc.Edit(item4);    Console.WriteLine("编辑后列出所有对象:");    sc.Items.ForEach(delegate (Collections.Item item)    {     Console.WriteLine(item.ToString());    });    var item5 = new Collections.Item();    item5.Key = 1;    sc.Delete(item5);    Console.WriteLine("删除后列出所有对象:");    sc.Items.ForEach(delegate (Collections.Item item)    {     Console.WriteLine(item.ToString());    });

运行看看结果:

C#中集合Collections购物车Shopping Cart的示例分析

感谢你能够认真阅读完这篇文章,希望小编分享的“C#中集合Collections购物车Shopping Cart的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI