国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#集合Collections購物車Shopping Cart(實例講解)

2020-01-24 00:22:32
字體:
來源:轉載
供稿:網友

這篇是對象與集合操練,物件的創建,集合的一些基本功能,如添加,編輯,刪除等功能。

對象,即是網店的商品物件,Insus.NET只為其添加2個屬性,物件的ID的Key和名稱ItemName以及2個構造函數,最后一個方法是重寫ToString()方法。

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:

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

下面是在控制臺測試上面寫好的集合購物車:


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輸出結果:

最后演示編輯Edit和刪除Delete的功能:

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(實例講解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蚌埠市| 米泉市| 浙江省| 凤阳县| 南投市| 康马县| 崇阳县| 绥滨县| 三亚市| 新安县| 馆陶县| 齐齐哈尔市| 淳化县| 晋州市| 东乡族自治县| 安泽县| 蛟河市| 秭归县| 乐安县| 朝阳市| 泽普县| 高平市| 蕉岭县| 新化县| 景泰县| 宣恩县| 双城市| 南丰县| 辽中县| 大田县| 尼勒克县| 本溪| 沙湾县| 和田市| 广平县| 沭阳县| 大丰市| 瑞安市| 株洲县| 怀集县| 襄城县|