最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#集合Collections購(gòu)物車Shopping Cart(實(shí)例講解)

 更新時(shí)間:2017年12月15日 09:33:35   作者:楊明波(Leo Yang)  
下面小編就為大家分享一篇C#集合Collections購(gòu)物車Shopping Cart的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

對(duì)象,即是網(wǎng)店的商品物件,Insus.NET只為其添加2個(gè)屬性,物件的ID的Key和名稱ItemName以及2個(gè)構(gòu)造函數(shù),最后一個(gè)方法是重寫(xiě)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);
  }
 }

有了物件,你可以創(chuàng)建你的購(gòu)物車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 //集合中物件數(shù)量
  {
   get
   {
    return this._sl.Count;
   }
  }

  public virtual IEnumerable<Item> Items //獲取所有物件
  {
   get
   {
    return this._sl.Values;
   }
  }
 }

下面是在控制臺(tái)測(cè)試上面寫(xiě)好的集合購(gòu)物車:


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("使用索引器,輸出對(duì)象:");
   Console.WriteLine(sc[3].ToString());

   Console.WriteLine("集合中對(duì)象數(shù)量:");
   Console.WriteLine(sc.Count);

   Console.WriteLine("列出所有對(duì)象:");
   sc.Items.ForEach(delegate (Collections.Item item)
   {
    Console.WriteLine(item.ToString());
   });
  }
 }

按Ctrl + F5輸出結(jié)果:

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

var item4 = new Collections.Item();
   item4.Key = 2;
   item4.ItemName = "Huawei Mate10";
   sc.Edit(item4);

   Console.WriteLine("編輯后列出所有對(duì)象:");
   sc.Items.ForEach(delegate (Collections.Item item)
   {
    Console.WriteLine(item.ToString());
   });


   var item5 = new Collections.Item();
   item5.Key = 1;
   sc.Delete(item5);

   Console.WriteLine("刪除后列出所有對(duì)象:");
   sc.Items.ForEach(delegate (Collections.Item item)
   {
    Console.WriteLine(item.ToString());
   });

運(yùn)行看看結(jié)果:

以上這篇C#集合Collections購(gòu)物車Shopping Cart(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

溧水县| 南乐县| 宁乡县| 江陵县| 抚州市| 黑山县| 马山县| 武川县| 宜章县| 泽普县| 北宁市| 社旗县| 高邑县| 桦甸市| 五莲县| 晋宁县| 合水县| 凌源市| 兴化市| 柳河县| 五河县| 治县。| 东兰县| 翁源县| 尉氏县| 仙桃市| 如东县| 鸡东县| 澜沧| 家居| 依兰县| 贺兰县| 平江县| 内江市| 通州区| 南投市| 通江县| 原阳县| 五常市| 杭锦后旗| 平原县|