20121009 P85
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相关推荐
- win11 P85主板能正常安装吗 windows11使用p85主板的安装的步骤方法
P85主板相信有不少小伙伴的电脑都有在使用,因为Win11系统的升级对主板也是有一定要求的,因此使用P85主板小伙伴就会想知道自己的主板到底符不符合要求,那么今天就来看一看小编整理的资料了解下吧.更多 ...
- (P85)stl(十三):容器适配器,stack,queue,优先级队列priority_queue,make_heap
文章目录 1.容器适配器 2.stack 3.queue 4.优先级队列priority_queue 5.make_heap 6.set 1.容器适配器 利用基本容器构造的容器,称之为容器适配器 基本 ...
- python学习基础篇Day08(P85~~95)
b站达内python课学习笔记 P85 Python基础-8.1 课程内容回顾 一.day07复习 """day07 复习能力提升for for# 结论:外层循环执行一次 ...
- Leetcode P85 Java
Leetcode P85 Java 执行用时:8 ms, 在所有 Java 提交中击败了73.52%的用户 内存消耗:46.4 MB, 在所有 Java 提交中击败了10.50%的用户 通过测试用例: ...
- 谷粒商城P85问题记录—发布商品时规格参数不显示-2022/4/8
谷粒商城P85问题记录-发布商品时规格参数不显示 这一p有2个问题,折腾了很久 问题1 :数据库表中不存在 valueType这个键 但是接口文档里是需要提供这个键(而且是不能为null) 所以需要: ...
- 谷粒商城P85单选和多选无法修改问题
谷粒商城P85单选和多选无法修改问题 问题:数据库表中不存在 value_Type这个字段 接口文档里是需要提供value_Type这个键(而且是不能为null) 解决方法: 1.在数据库表pms_a ...
- 【李宏毅2020 ML/DL】P85 Transfer Learning
我已经有两年 ML 经历,这系列课主要用来查缺补漏,会记录一些细节的.自己不知道的东西. 本节内容综述 要做一项任务,但是数据不直接与任务相关.这就涉及到了迁移学习.在现实生活中,我们其实不断在做&q ...
- P85.2.(2)回文是指正读反读均相同的字符序列,如“abba”和“abdba”均是回文,但“good”不是回文。试写一个算法判定给定的字符序列是否是回文。(提示:将一半的字符入栈)(C语言描述)
做这个题时遇到个bug,即main()中的第一句,我定义了指针str_input用来装输入的字符串,但是后面赋值操作却用了get(),如果要用get()读入字符串的话就应该将str_input定义为数 ...
- pink老师 js p85思考题
var str = '';for (var i=1; i <=10; i++) { //外层循环负责打印五行for (var j =1; j <= i; j++) { //里层循环打印的个 ...
最新文章
- Android SDK Manager国内无法更新的解决方案
- IDEA入门(一):简介、安装
- iphone闪退修复工具_支持iOS13~13.3越狱工具发布(附下载地址)
- android guide 中文,Android API Guide:Toast 中文翻译
- CentOS 安装gnutls
- c 连接oracle 通用类,c#操作oracle,有没有相仿sqlhelp之类的通用操作类(6)
- RPi 2B Raspbian system install
- type=file的未选择任何文件修改_Electron应用易“招黑”,轻松被修改并植入后门...
- uni-app简单介绍
- 一种简单的DWG在线浏览方法
- php提交表单怎么验证必填,PHP 表单验证 - 必填字段
- 神秘诡异的量子世界是如何毁掉科学家三观的?
- 数据库服务器虚拟内存设置
- 单细胞测序数据下载和预处理
- html5中将图片的绝对路径转换成文件对象
- 计算机服务flash,Flash Player右下角弹广告flash helper service解决教程
- Java项目:SSH自驾游管理系统
- 初识Matlab以及Matlab一般性教程
- 青岛大学计算机小姐姐,青岛大学举牌校花一夜爆红,评论区留下脏话:见不得别人好是病!...
- 行列式某一行的元素与另一行对应元素的代数余子式的乘积之和等于0
热门文章
- 炫酷大屏demo_20套大屏模板,教你3分钟制作出酷炫的可视化大屏
- php网页视频播放插件下载_php 网页播放器用来播放在线视频的代码(自动判断并选择视频文件类型)...
- 联盟不显示聊天服务器未响应,玩英雄联盟出现程序未响应是怎么回事
- 微信小程序ssm家校通系统-家校联系系统app
- 必应词典android版的一个bug
- 在EXCEL中一个窗口显示多个工作表
- Python抓取网页中的动态序列化数据
- Python day3 Python中raw字符串与多行字符串
- 素诺多功能可视采耳棒实际体验
- 基于java的城市公交查询系统