20121009 P85
来源:互联网 发布:发际线越来越高 知乎 编辑:程序博客网 时间:2024/05/22 16:07
Essential Language Features
扩展方法:
1、下面是通过静态类的静态属性以及this来实现单独一个类的方法来的扩展
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ using System.Collections.Generic; public class Product { public int ProductID { get; set; } private string name; public string Description { get; set;} public decimal Price { get; set; } public string Category { set; get;} public string Name { get { return ProductID + name;} set { name = value; } } } public class ShoppingCart { public List<Product> Products { get; set; } } /// <summary> /// 下面的参数是带this的,类是静态类,怎么扩展一个类的方法 /// </summary> public static class MyExtensionMethods { public static decimal TotalPrices(this ShoppingCart cartParam) { decimal total = 0; foreach (Product prod in cartParam.Products) { total += prod.Price; } return total; } } class Program { static void Main(string[] args) { // create and populate ShoppingCart ShoppingCart cart = new ShoppingCart { Products = new List<Product> { new Product {Name = "Kayak", Price = 275M}, new Product {Name = "Lifejacket", Price = 48.95M}, new Product {Name = "Soccer ball", Price = 19.50M}, new Product {Name = "Corner flag", Price = 34.95M} } }; // get the total value of the products in the cart decimal cartTotal = cart.TotalPrices(); Console.WriteLine("Total: {0:c}", cartTotal); Console.ReadKey(); } }}
利用this和静态类来实现方法的扩展。this标志了TotalPrices方法所在的类。不好的地方是只能用在一个类中,也就是只能指定一个类。所以我们通过接口避免此弊端。
2、通过接口实现扩展方法
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ using System.Collections.Generic; using System.Collections; public class Product { public int ProductID { get; set; } private string name; public string Description { get; set;} public decimal Price { get; set; } public string Category { set; get;} public string Name { get { return ProductID + name;} set { name = value; } } } public class ShoppingCart : IEnumerable<Product> { public List<Product> Products { get; set; } public IEnumerator<Product> GetEnumerator() { return Products.GetEnumerator();//对Products进行迭代获取Product列表 } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } /// <summary> /// 下面的参数是带this的,类是静态类,怎么扩展一个类的方法 /// </summary> public static class MyExtensionMethods { public static decimal TotalPrices(this IEnumerable<Product> productEnum) { decimal total = 0; foreach (Product prod in productEnum) { total += prod.Price; } return total; } } class Program { static void Main(string[] args) { IEnumerable<Product> products = new ShoppingCart { Products = new List<Product> { new Product {Name = "Kayak", Price = 275M}, new Product {Name = "Lifejacket", Price = 48.95M}, new Product {Name = "Soccer ball", Price = 19.50M}, new Product {Name = "Corner flag", Price = 34.95M} } }; //数组也实现了枚举类型的接口。 Product[] productArray = { new Product {Name = "Kayak", Price = 275M}, new Product {Name = "Lifejacket", Price = 48.95M}, new Product {Name = "Soccer ball", Price = 19.50M}, new Product {Name = "Corner flag", Price = 34.95M} }; decimal cartTotal = products.TotalPrices(); decimal arrayTotal = productArray.TotalPrices(); Console.WriteLine("Cart Total: {0:c}", cartTotal); Console.WriteLine("Array Total: {0:c}", arrayTotal); Console.ReadKey(); } }}
数组实现IEnumerable<Product>接口是要注意的地方。除此之外,该方法还能带额外的参数。
3、创造过滤方法
在静态类中添加代码
public static IEnumerable<Product> FilterByCategory( this IEnumerable<Product> productEnum, string categoryParam) { foreach (Product prod in productEnum) { if (prod.Category == categoryParam) { yield return prod; } } }
在main函数中,添加代码:
IEnumerable<Product> products1 = new ShoppingCart { Products = new List<Product> { new Product {Name = "Kayak", Category = "Watersports", Price = 275M}, new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M}, new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M}, new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M} } }; foreach (Product prod in products1.FilterByCategory("Soccer")) { Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price); }
4.lamda表达式
- 20121009 P85
- P85
- P85--94 notes
- 20121008 P60-P85
- 算法导论P85 快速排序
- C Primer Plus 练习 P85
- 严蔚敏数据结构第二版(p85,3)
- W. :快速排序(利用分治法). 7.1,P85
- 《算法导论》CLRS算法C++实现(四)P85 快速排序
- 数据结构教程(第四版)P85~87//队列的链式存储结构
- 阿里巴巴20121009 研发/算法工程师 笔试试题
- 阿里巴巴20121009 研发/算法工程师 笔试试题【修正】
- Jquery取得iframe中元素的几种方法(转载)
- matlab数据类型和转换
- PHP快速开发工具箱:轻松解决PHP日常编程问题的100个插件,在线
- AddressBook 地址簿 (电话簿) 访问与修改-IOS开发
- 漾七夕
- 20121009 P85
- LINQ学习心得分享--------(二)LINQ语法详解
- 天猫忌惮京东开放平台壮大 欲借“二选一”形成垄断
- Qt移植到开发板上改变字体的大小
- APUE 第八章 进程管理 子进程领养问题探讨
- [游泳] developing a good mindset when learning freestyle
- Hibernate SQL 方言(hibernate.dialect)
- 软中断_硬中断 区别
- SQL中delete删除多表数据的进一步认识