From a7aab33845e62d2a363b7f5c6008e68d7c9456b4 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sun, 9 Jun 2019 03:48:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A1=E7=AE=97=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Calculator/Addition.cs | 20 +++++ .../Calculator/Division.cs | 23 +++++ .../Calculator/Multiplication.cs | 23 +++++ .../Calculator/Subtraction.cs | 23 +++++ Study.DelegateSeries.Core/Demo.cs | 30 +++++++ Study.DelegateSeries.Core/Person/Student.cs | 26 ++++++ Study.DelegateSeries.Core/Person/Teacher.cs | 26 ++++++ .../Study.DelegateSeries.Core.csproj | 2 +- Study.DelegateSeries.Core/StudyDelegate.cs | 84 ++++++++++++++++++- .../wwwroot/DelegateStudy.html | 41 ++++++++- .../wwwroot/DelegateStudy.md | 43 +++++++++- Study.DelegateSeries.Test/DemoTest.cs | 21 +++++ .../Study.DelegateSeries.Test.csproj | 4 + 13 files changed, 362 insertions(+), 4 deletions(-) create mode 100644 Study.DelegateSeries.Core/Calculator/Addition.cs create mode 100644 Study.DelegateSeries.Core/Calculator/Division.cs create mode 100644 Study.DelegateSeries.Core/Calculator/Multiplication.cs create mode 100644 Study.DelegateSeries.Core/Calculator/Subtraction.cs create mode 100644 Study.DelegateSeries.Core/Demo.cs create mode 100644 Study.DelegateSeries.Core/Person/Student.cs create mode 100644 Study.DelegateSeries.Core/Person/Teacher.cs create mode 100644 Study.DelegateSeries.Test/DemoTest.cs diff --git a/Study.DelegateSeries.Core/Calculator/Addition.cs b/Study.DelegateSeries.Core/Calculator/Addition.cs new file mode 100644 index 0000000..8fdeaea --- /dev/null +++ b/Study.DelegateSeries.Core/Calculator/Addition.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Study.DelegateSeries.Core.Calculator +{ + /// + /// 加法类 + /// + public class Addition + { + /// + /// 求两个数的和 + /// + public decimal Add(decimal first,decimal second) + { + return first + second; + } + } +} diff --git a/Study.DelegateSeries.Core/Calculator/Division.cs b/Study.DelegateSeries.Core/Calculator/Division.cs new file mode 100644 index 0000000..8e72b22 --- /dev/null +++ b/Study.DelegateSeries.Core/Calculator/Division.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Study.DelegateSeries.Core.Calculator +{ + /// + /// 除法类 + /// + public class Division + { + /// + /// 求商 + /// + /// 被除数 + /// 除数 + /// + public decimal af(decimal dividend, decimal divisor) + { + return dividend / divisor; + } + } +} diff --git a/Study.DelegateSeries.Core/Calculator/Multiplication.cs b/Study.DelegateSeries.Core/Calculator/Multiplication.cs new file mode 100644 index 0000000..6ae2272 --- /dev/null +++ b/Study.DelegateSeries.Core/Calculator/Multiplication.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Study.DelegateSeries.Core.Calculator +{ + /// + /// 乘法类 + /// + public class Multiplication + { + /// + /// 求积 + /// + /// 因数A + /// 因数B + /// + public decimal Multiplicat(decimal factorA,decimal factorB) + { + return factorA * factorB; + } + } +} diff --git a/Study.DelegateSeries.Core/Calculator/Subtraction.cs b/Study.DelegateSeries.Core/Calculator/Subtraction.cs new file mode 100644 index 0000000..4da09f7 --- /dev/null +++ b/Study.DelegateSeries.Core/Calculator/Subtraction.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Study.DelegateSeries.Core.Calculator +{ + /// + /// 减法类 + /// + public class Subtraction + { + /// + /// 求差 + /// + /// 被减数 + /// 减数 + /// + public decimal Difference(decimal minuend, decimal subtractor) + { + return minuend - subtractor; + } + } +} diff --git a/Study.DelegateSeries.Core/Demo.cs b/Study.DelegateSeries.Core/Demo.cs new file mode 100644 index 0000000..4f7bde7 --- /dev/null +++ b/Study.DelegateSeries.Core/Demo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Study.DelegateSeries.Core +{ + public delegate int mydelegate(); + public class Demo + { + + public int MyDemo(int b) + { + return b * b; + } + + public int UseDelegate() + { + mydelegate d = delegate () { return 5; }; + + var total = MyDemo(d()); + + return total; + } + + private void aa() + { + + } + } +} diff --git a/Study.DelegateSeries.Core/Person/Student.cs b/Study.DelegateSeries.Core/Person/Student.cs new file mode 100644 index 0000000..f6b4076 --- /dev/null +++ b/Study.DelegateSeries.Core/Person/Student.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Study.DelegateSeries.Core +{ + /// + /// 学生类 + /// + public class Student + { + public int Id { get; set; } + + public string Name { get; set; } + + public static string ShowTypeName() + { + return typeof(Student).FullName; + } + + public string ShowStudentName() + { + return this.Name; + } + } +} diff --git a/Study.DelegateSeries.Core/Person/Teacher.cs b/Study.DelegateSeries.Core/Person/Teacher.cs new file mode 100644 index 0000000..fa0d3d7 --- /dev/null +++ b/Study.DelegateSeries.Core/Person/Teacher.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Study.DelegateSeries.Core +{ + /// + /// 老师类 + /// + public class Teacher + { + public int Id { get; set; } + + public string Name { get; set; } + + public static string ShowTypeName() + { + return typeof(Teacher).FullName; + } + + public string ShowTeacherName() + { + return this.Name; + } + } +} diff --git a/Study.DelegateSeries.Core/Study.DelegateSeries.Core.csproj b/Study.DelegateSeries.Core/Study.DelegateSeries.Core.csproj index 9f5c4f4..dbdcea4 100644 --- a/Study.DelegateSeries.Core/Study.DelegateSeries.Core.csproj +++ b/Study.DelegateSeries.Core/Study.DelegateSeries.Core.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 diff --git a/Study.DelegateSeries.Core/StudyDelegate.cs b/Study.DelegateSeries.Core/StudyDelegate.cs index 2de9493..2d3f0eb 100644 --- a/Study.DelegateSeries.Core/StudyDelegate.cs +++ b/Study.DelegateSeries.Core/StudyDelegate.cs @@ -1,9 +1,11 @@ -using System; +using Study.DelegateSeries.Core.Calculator; +using System; namespace Study.DelegateSeries.Core { /// /// 委托学习 + /// 使用:定义委托、实例化委托、调用委托 /// public class StudyDelegate { @@ -21,5 +23,85 @@ namespace Study.DelegateSeries.Core * 10、总结 */ + #region 定义委托 + //基本语法 + public delegate decimal OperationDelegate(decimal a,decimal b); + + public delegate string ShowTypeNameDelegate(); + + public delegate string ShowNameDelegate(); + #endregion + + #region 实例化委托 + + /// + /// 基础语法 + /// (c#1.0 开始提供) + /// + public OperationDelegate BaseInstance() + { + OperationDelegate operation = new OperationDelegate(new Addition().Add); + return operation; + } + + /// + /// c#2.0 简写方法 + /// (基于类型推断) + /// + public OperationDelegate AutoTypeInstance() + { + OperationDelegate operation = new Subtraction().Difference; + return operation; + } + + /// + /// c#2.0 匿名委托 + /// (使用匿名方法实例化的委托) + /// + public OperationDelegate AnonymousInstance() + { + //c# 2.0基础语法 + OperationDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); }; + return operation; + } + + /// + /// c#3.0 Lambda表达式 + /// (实质:"Lambda表达式"是一个匿名函数) + /// + public OperationDelegate LambdaInstance() + { + OperationDelegate operation = (decimal a,decimal b)=> { return a * a + b * b + 2 * a * b; }; + //自动推断 + OperationDelegate operation2 = (a,b) => { return a * a + b * b + 2 * a * b; }; + + //{}只有一句时的简写 + OperationDelegate operation3 = (a, b) => a * a + b * b + 2 * a * b ; + + return operation; + } + + #endregion + + #region 调用委托 + #endregion + + #region 闭包 + #endregion + + #region 多播委托 + #endregion + + #region 泛型委托 + #endregion + + #region 内置委托 + #endregion + + #region 委托与事件 + #endregion + + #region 委托与接口 + #endregion } } diff --git a/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.html b/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.html index 333d5b7..c50522e 100644 --- a/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.html +++ b/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.html @@ -17,7 +17,7 @@

委托和类一样,是一种用于封装命名或匿名方法的用户自定义引用类型。 委托和类同级,类表示数据集合,委托表示对一个或多个方法的引用,储存的是一系列具有相同参数和返回类型方法的地址列表,调用委托时,此委托列表的所有方法都将被执行。

-

作用理解:

+

语言框架角度:

委托是.net中函数回调机制的实现方式。 是函数指针在面向对象中的封装, @@ -29,6 +29,45 @@

  • 类型安全
  • 可靠
  • +

    委托演进

    +
      +
    1. c# 1.0:基本委托语法
    2. +
    3. c# 2.0:实例化可省略new 与 匿名方法
    4. +
    5. c# 3.0及以上版本:lambda 表达式(=> 读作 "goes to")
    6. +
    7. c# 3.0之后
    8. +
    +

    委托使用场景

    +

    使用事件或事件设计模式时

    +
    +

    就是Observer设计模式,它定义了对象之间一对多的关系,并且通过事件触发机制关联它们。当一个对象中发生了某事件后,依赖它的其他对象会被自动触发并更新。

    +
    +

    需要封装静态方法时

    +
    +

    委托绑定的方法可以是静态方法、非静态方法和匿名方法,而C#中接口不能是静态的。

    +
    +

    调用方不需要访问实现该方法的对象中的其他属性、方法或接口时

    +
    +

    类中的某个成员函数绑定到委托,调用该方法时只与这个成员函数有关,与该类里的其他属性无关。

    +
    +

    需要方便的组合时

    +
    +

    一个委托绑定的多个方法可以自由组合。一个委托的对象可以绑定多个方法,而且这多个方法是没有限制可以任意组合的,委托灵活的绑定和解绑定策略使得使用非常方便。

    +
    +

    类可能需要该方法的多个实现时

    +
    +

    一个委托的对象可以绑定多个方法(多播委托),当我们运行时需要的不是单一的方法时,接口很难实现。

    +
    +

    委托使用步骤:

    +

    1、定义委托

    +
    
    +
    +

    2、实例化委托

    +
    
    +
    +
    +

    3、调用(执行)委托

    +
    
    +
    diff --git a/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.md b/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.md index a87014e..4f0301f 100644 --- a/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.md +++ b/Study.DelegateSeries.MarkdownDoc/wwwroot/DelegateStudy.md @@ -12,7 +12,7 @@ > 委托和类一样,是一种用于封装命名或匿名方法的用户自定义引用类型。 委托和类同级,类表示数据集合,委托表示对一个或多个方法的引用,储存的是一系列具有相同参数和返回类型方法的地址列表,调用委托时,此委托列表的所有方法都将被执行。 -作用理解: +语言框架角度: > 委托是.net中函数回调机制的实现方式。 > 是函数指针在面向对象中的封装, @@ -23,5 +23,46 @@ * 类型安全 * 可靠 +委托演进 +---------- +1. c# 1.0:基本委托语法 +2. c# 2.0:实例化可省略new 与 匿名方法 +3. c# 3.0及以上版本:lambda 表达式(=> 读作 "goes to") +4. c# 3.0之后 + +委托使用场景 +----------- +**使用事件或事件设计模式时** +> 就是Observer设计模式,它定义了对象之间一对多的关系,并且通过事件触发机制关联它们。当一个对象中发生了某事件后,依赖它的其他对象会被自动触发并更新。 + +**需要封装静态方法时** +> 委托绑定的方法可以是静态方法、非静态方法和匿名方法,而C#中接口不能是静态的。 + +**调用方不需要访问实现该方法的对象中的其他属性、方法或接口时** +> 类中的某个成员函数绑定到委托,调用该方法时只与这个成员函数有关,与该类里的其他属性无关。 + +**需要方便的组合时** +> 一个委托绑定的多个方法可以自由组合。一个委托的对象可以绑定多个方法,而且这多个方法是没有限制可以任意组合的,委托灵活的绑定和解绑定策略使得使用非常方便。 + +**类可能需要该方法的多个实现时** +> 一个委托的对象可以绑定多个方法(**多播委托**),当我们运行时需要的不是单一的方法时,接口很难实现。 + + +委托使用步骤: +-------------- +### 1、定义委托 ### +```c sharp + +``` +### 2、实例化委托 ### +``` c# + + +``` + +### 3、调用(执行)委托 ### +``` c# + +``` diff --git a/Study.DelegateSeries.Test/DemoTest.cs b/Study.DelegateSeries.Test/DemoTest.cs new file mode 100644 index 0000000..a9f5165 --- /dev/null +++ b/Study.DelegateSeries.Test/DemoTest.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +using Study.DelegateSeries.Core; + +namespace Study.DelegateSeries.Test +{ + public class DemoTest + { + [Fact] + public void Test1() + { + var _demo = new Demo(); + var total = _demo.UseDelegate(); + + Assert.Equal(25, total); + } + } +} diff --git a/Study.DelegateSeries.Test/Study.DelegateSeries.Test.csproj b/Study.DelegateSeries.Test/Study.DelegateSeries.Test.csproj index 501a5ce..10f5e03 100644 --- a/Study.DelegateSeries.Test/Study.DelegateSeries.Test.csproj +++ b/Study.DelegateSeries.Test/Study.DelegateSeries.Test.csproj @@ -12,4 +12,8 @@ + + + +