C#集合Collections購(gòu)物車(chē)Shopping Cart(實(shí)例講解)
這篇是對(duì)象與集合操練,物件的創(chuàng)建,集合的一些基本功能,如添加,編輯,刪除等功能。
對(duì)象,即是網(wǎng)店的商品物件,Insus.NET只為其添加2個(gè)屬性,物件的ID的Key和名稱(chēng)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)物車(chē)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)物車(chē):
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)物車(chē)Shopping Cart(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。
上一篇:C# out關(guān)鍵詞的應(yīng)用實(shí)例
欄 目:C#教程
下一篇:基于JWT.NET的使用(詳解)
本文標(biāo)題:C#集合Collections購(gòu)物車(chē)Shopping Cart(實(shí)例講解)
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5330.html
您可能感興趣的文章
- 01-10C#操作IIS方法集合
- 01-10C#實(shí)現(xiàn)過(guò)濾sql特殊字符的方法集合
- 01-10C#基于cookie實(shí)現(xiàn)的購(gòu)物車(chē)功能
- 01-10詳解C#中使用對(duì)象或集合的初始值設(shè)定項(xiàng)初始化的操作
- 01-10C#常見(jiàn)的幾種集合 ArrayList,Hashtable,List&lt;T&gt;,
- 01-10C#中Dictionary泛型集合7種常見(jiàn)的用法
- 01-10C#中判斷一個(gè)集合是否是另一個(gè)集合的子集的簡(jiǎn)單方法
- 01-10C#后臺(tái)接受前臺(tái)JSON字符串裝換成字典集合處理
- 01-10C#遍歷集合與移除元素的方法
- 01-10C#集合遍歷時(shí)刪除和增加元素的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子


