这篇是对象与集合操练,物件的创建,集合的一些基本功能,如添加,编辑,删除等功能。

对象,即是网店的商品物件,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);
}
}

Source Code

有了物件,你可以创建你的购物车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;
}
}
}

Source Code

下面是在控制台测试上面写好的集合购物车:

 class Program
{
static void Main(string[] args)
{
ShoppingCart sc = new ShoppingCart(); var item1 = new Collections.Item();
item1.Key = ;
item1.ItemName = "Huawei V8";
sc.Add(item1); var item2 = new Collections.Item();
item2.Key = ;
item2.ItemName = "Huawei V9";
sc.Add(item2); var item3 = new Collections.Item();
item3.Key = ;
item3.ItemName = "Huawei V10";
sc.Add(item3); Console.WriteLine("使用索引器,输出对象:");
Console.WriteLine(sc[].ToString()); Console.WriteLine("集合中对象数量:");
Console.WriteLine(sc.Count); Console.WriteLine("列出所有对象:");
sc.Items.ForEach(delegate (Collections.Item item)
{
Console.WriteLine(item.ToString());
});
}
}

Source Code

按Ctrl + F5输出结果:

最后演示编辑Edit和删除Delete的功能:

var item4 = new Collections.Item();
item4.Key = ;
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 = ;
sc.Delete(item5); Console.WriteLine("删除后列出所有对象:");
sc.Items.ForEach(delegate (Collections.Item item)
{
Console.WriteLine(item.ToString());
});

Source Code

运行看看结果:

C#集合Collections购物车Shopping Cart的更多相关文章

  1. 购物车(Shopping cart) —— B2C网站核心产品设计 (二)

    购物车是做什么的? 我们先来看一下现实超市中的购物车,一个带四个轱辘的铁筐子,客人推来推去,看到什么东西喜欢,就扔进去,觉得东西差不多了,就推到收银台. 那B2C网站中的购物车又是一个什么东西呢? 从 ...

  2. shopping cart

    #Author:Kevin_hou #定义产品列表 product_list =[ ('HUAWEI',5999), ('Watch',500), ('Nike',800), ('Toyota',20 ...

  3. Backbone.js 为复杂Javascript应用程序提供模型(models)、集合(collections)、视图(views)的结构

    Backbone.js 为复杂Javascript应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和 自定义事件:集合附有可枚举函数 ...

  4. Java集合——Collections工具类

    Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...

  5. 集合-Collections工具

    1.定义 Collections是集合类的一个工具类,它提供了一系列静态方法用于对容器中的元素进行排序和搜索等一系列操作. 注:Collection是一个集合接口,而Collections是一个有着操 ...

  6. Guava 3: 集合Collections

    一.引子 Guava 对JDK集合的拓展,是最成熟且最受欢迎的部分.本文属于Guava的核心,需要仔细看. 二.Guava 集合 2.1 Immutable Collections不可变集合 1.作用 ...

  7. Java 集合-Collections工具类

    2017-11-05 23:41:53 Collections类 Collections类:Collections类是针对集合进行操作的工具类,都是静态方法. 常用方法: public static ...

  8. Java中的集合Collections工具类(六)

    操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...

  9. java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合

    Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...

随机推荐

  1. ionic cordova 安装指定版本

    安装ionic 及 cordova npm install -g cordova ionic npm 淘宝镜像(GFW,导致很多插件下载失败) npm install -g cnpm --regist ...

  2. 网络基础 http 会话(session)详解

    http 会话(session)详解 by:授客 QQ:1033553122 会话(session)是一种持久网络协议,在用户(或用户代理)端和服务器端之间创建关联,从而起到交换数据包的作用机制 一. ...

  3. loadrunner 脚本开发-定义全局变量

    脚本开发-定义全局变量 by:授客 QQ:1033553122 如果参数是全局的,在脚本中的任何一个Action中都可以使用,变量一般是局部的,如果跨Action调用会出现未声明的错误. 打开Scri ...

  4. Android Stuido代码混淆

    一.Android Studio 代码混淆基本配置首先我们要在build.gradle里设置 miifyEnabled 里改为true,表示可以混淆 proguardFiles getDefaultP ...

  5. Android basics

    只要是Android中的控件,最终都继承自View.

  6. CSS中各种长度单位总结

    在前端开发工作过程中曾碰到这样一问题: <style type="text/css"> .parent{ width:400px; height:300px; bord ...

  7. 小技巧-mac修改finder菜单栏

    效果: 方法: 添加:打开finder后,长按command,可以将其他app拖到菜单栏. 删除:同理,长按command,将不需要的图标拖出菜单栏即可. PS:强烈推荐gotoshell这个小工具, ...

  8. JDBC-Statement,prepareStatement,CallableStatement的比较

    参考:https://www.cnblogs.com/Lxiaojiang/p/6708570.html JDBC核心API提供了三种向数据库发送SQL语句的类: Statement:使用create ...

  9. Java中当前对象引用

    题: 计算机画图时,有点的概念,每个点由它的横坐标x 和 纵坐标 y 描述. 写一个类. 求两个点之间的曼哈顿距离 = 横向距离 + 纵向距离 例如,一个点(0,0) 和另一个点(1,1)的曼哈顿距离 ...

  10. late_initcall和module_init的区别

    在init.h中有如下定义: 详情参照:linux 设备驱动加载的先后顺序 #define pure_initcall(fn) __define_initcall("0",fn,1 ...