C#集合Collections購(gòu)物車Shopping Cart(實(shí)例講解)
這篇是對(duì)象與集合操練,物件的創(chuàng)建,集合的一些基本功能,如添加,編輯,刪除等功能。
對(duì)象,即是網(wǎng)店的商品物件,Insus.NET只為其添加2個(gè)屬性,物件的ID的Key和名稱ItemName以及2個(gè)構(gòu)造函數(shù),最后一個(gè)方法是重寫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è)試上面寫好的集合購(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)文章
詳解.NET 6如何實(shí)現(xiàn)獲取當(dāng)前登錄用戶信息
這篇文章主要介紹了.NET 6在應(yīng)用開發(fā)時(shí)是如何實(shí)現(xiàn)當(dāng)前登陸用戶信息獲取的,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-01-01
C#實(shí)現(xiàn)3步手動(dòng)建DataGridView的方法
這篇文章主要介紹了C#實(shí)現(xiàn)3步手動(dòng)建DataGridView的方法,實(shí)例分析了C#實(shí)現(xiàn)手動(dòng)創(chuàng)建DataGridView的原理與技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
WPF實(shí)現(xiàn)監(jiān)聽快捷鍵的方式分享
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)監(jiān)聽快捷鍵的幾種方式,文中的示例代碼講解詳細(xì),具有一定的借鑒與學(xué)習(xí)價(jià)值,需要的可以了解一下2023-03-03
WCF基礎(chǔ)介紹并創(chuàng)建簡(jiǎn)單應(yīng)用程序
這篇文章介紹了WCF基礎(chǔ)并創(chuàng)建簡(jiǎn)單WCF應(yīng)用程序,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
使用C#開發(fā)OPC?Server服務(wù)器源碼解析
OPC?Server服務(wù)器服務(wù)器的開發(fā)比較繁瑣,本示例采用C#提供了一種簡(jiǎn)單快速實(shí)現(xiàn)OPCServer的方法,已經(jīng)在工程項(xiàng)目中應(yīng)用,本文對(duì)C#開發(fā)OPC?Server服務(wù)器相關(guān)知識(shí)給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06

