欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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


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("集合中對象數(shù)量:");
   Console.WriteLine(sc.Count);

   Console.WriteLine("列出所有對象:");
   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("編輯后列出所有對象:");
   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());
   });

運行看看結(jié)果:

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

相關(guān)文章

  • 詳解.NET 6如何實現(xiàn)獲取當(dāng)前登錄用戶信息

    詳解.NET 6如何實現(xiàn)獲取當(dāng)前登錄用戶信息

    這篇文章主要介紹了.NET 6在應(yīng)用開發(fā)時是如何實現(xiàn)當(dāng)前登陸用戶信息獲取的,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-01-01
  • 快速了解c# 常量

    快速了解c# 常量

    這篇文章主要介紹了c# 常量的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#使用dynamic類型訪問JObject對象

    C#使用dynamic類型訪問JObject對象

    這篇文章主要為大家詳細(xì)介紹了C#使用dynamic類型訪問JObject對象,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • C#實現(xiàn)3步手動建DataGridView的方法

    C#實現(xiàn)3步手動建DataGridView的方法

    這篇文章主要介紹了C#實現(xiàn)3步手動建DataGridView的方法,實例分析了C#實現(xiàn)手動創(chuàng)建DataGridView的原理與技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • C#實現(xiàn)簡單的二叉查找樹

    C#實現(xiàn)簡單的二叉查找樹

    這篇文章介紹了C#實現(xiàn)二叉查找樹的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • WPF實現(xiàn)監(jiān)聽快捷鍵的方式分享

    WPF實現(xiàn)監(jiān)聽快捷鍵的方式分享

    這篇文章主要為大家詳細(xì)介紹了WPF實現(xiàn)監(jiān)聽快捷鍵的幾種方式,文中的示例代碼講解詳細(xì),具有一定的借鑒與學(xué)習(xí)價值,需要的可以了解一下
    2023-03-03
  • C#中泛型類和擴(kuò)展方法如何使用

    C#中泛型類和擴(kuò)展方法如何使用

    這篇文章介紹了C#中泛型類和擴(kuò)展方法的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • WCF基礎(chǔ)介紹并創(chuàng)建簡單應(yīng)用程序

    WCF基礎(chǔ)介紹并創(chuàng)建簡單應(yīng)用程序

    這篇文章介紹了WCF基礎(chǔ)并創(chuàng)建簡單WCF應(yīng)用程序,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • 使用C#開發(fā)OPC?Server服務(wù)器源碼解析

    使用C#開發(fā)OPC?Server服務(wù)器源碼解析

    OPC?Server服務(wù)器服務(wù)器的開發(fā)比較繁瑣,本示例采用C#提供了一種簡單快速實現(xiàn)OPCServer的方法,已經(jīng)在工程項目中應(yīng)用,本文對C#開發(fā)OPC?Server服務(wù)器相關(guān)知識給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-06-06
  • 談?wù)刢#中的索引器

    談?wù)刢#中的索引器

    這篇文章主要介紹了c#中的索引器的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-09-09

最新評論