添加计算类

develop
bicijinlian 5 years ago
parent b08da88885
commit a7aab33845

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Study.DelegateSeries.Core.Calculator
{
/// <summary>
/// 加法类
/// </summary>
public class Addition
{
/// <summary>
/// 求两个数的和
/// </summary>
public decimal Add(decimal first,decimal second)
{
return first + second;
}
}
}

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Study.DelegateSeries.Core.Calculator
{
/// <summary>
/// 除法类
/// </summary>
public class Division
{
/// <summary>
/// 求商
/// </summary>
/// <param name="dividend">被除数</param>
/// <param name="divisor">除数</param>
/// <returns>商</returns>
public decimal af(decimal dividend, decimal divisor)
{
return dividend / divisor;
}
}
}

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Study.DelegateSeries.Core.Calculator
{
/// <summary>
/// 乘法类
/// </summary>
public class Multiplication
{
/// <summary>
/// 求积
/// </summary>
/// <param name="factorA">因数A</param>
/// <param name="factorB">因数B</param>
/// <returns>积</returns>
public decimal Multiplicat(decimal factorA,decimal factorB)
{
return factorA * factorB;
}
}
}

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Study.DelegateSeries.Core.Calculator
{
/// <summary>
/// 减法类
/// </summary>
public class Subtraction
{
/// <summary>
/// 求差
/// </summary>
/// <param name="minuend">被减数</param>
/// <param name="subtractor">减数</param>
/// <returns>差</returns>
public decimal Difference(decimal minuend, decimal subtractor)
{
return minuend - subtractor;
}
}
}

@ -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()
{
}
}
}

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Study.DelegateSeries.Core
{
/// <summary>
/// 学生类
/// </summary>
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;
}
}
}

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Study.DelegateSeries.Core
{
/// <summary>
/// 老师类
/// </summary>
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;
}
}
}

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>

@ -1,9 +1,11 @@
using System;
using Study.DelegateSeries.Core.Calculator;
using System;
namespace Study.DelegateSeries.Core
{
/// <summary>
/// 委托学习
/// 使用:定义委托、实例化委托、调用委托
/// </summary>
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 实例化委托
/// <summary>
/// 基础语法
/// (c#1.0 开始提供)
/// </summary>
public OperationDelegate BaseInstance()
{
OperationDelegate operation = new OperationDelegate(new Addition().Add);
return operation;
}
/// <summary>
/// c#2.0 简写方法
/// (基于类型推断)
/// </summary>
public OperationDelegate AutoTypeInstance()
{
OperationDelegate operation = new Subtraction().Difference;
return operation;
}
/// <summary>
/// c#2.0 匿名委托
/// (使用匿名方法实例化的委托)
/// </summary>
public OperationDelegate AnonymousInstance()
{
//c# 2.0基础语法
OperationDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); };
return operation;
}
/// <summary>
/// c#3.0 Lambda表达式
/// (实质:"Lambda表达式"是一个匿名函数)
/// </summary>
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
}
}

@ -17,7 +17,7 @@
<p>委托和类一样,是一种用于封装命名或匿名方法的用户自定义引用类型。
委托和类同级,类表示数据集合,委托表示对一个或多个方法的引用,储存的是一系列具有相同参数和返回类型方法的地址列表,调用委托时,此委托列表的所有方法都将被执行。</p>
</blockquote>
<p>作用理解</p>
<p>语言框架角度</p>
<blockquote>
<p>委托是.net中函数回调机制的实现方式。
是函数指针在面向对象中的封装,
@ -29,6 +29,45 @@
<li>类型安全</li>
<li>可靠</li>
</ul>
<h2 id="section-2">委托演进</h2>
<ol>
<li>c# 1.0:基本委托语法</li>
<li>c# 2.0实例化可省略new 与 匿名方法</li>
<li>c# 3.0及以上版本lambda 表达式(=&gt; 读作 &quot;goes to&quot;)</li>
<li>c# 3.0之后</li>
</ol>
<h2 id="section-3">委托使用场景</h2>
<p><strong>使用事件或事件设计模式时</strong></p>
<blockquote>
<p>就是Observer设计模式它定义了对象之间一对多的关系并且通过事件触发机制关联它们。当一个对象中发生了某事件后依赖它的其他对象会被自动触发并更新。</p>
</blockquote>
<p><strong>需要封装静态方法时</strong></p>
<blockquote>
<p>委托绑定的方法可以是静态方法、非静态方法和匿名方法而C#中接口不能是静态的。</p>
</blockquote>
<p><strong>调用方不需要访问实现该方法的对象中的其他属性、方法或接口时</strong></p>
<blockquote>
<p>类中的某个成员函数绑定到委托,调用该方法时只与这个成员函数有关,与该类里的其他属性无关。</p>
</blockquote>
<p><strong>需要方便的组合时</strong></p>
<blockquote>
<p>一个委托绑定的多个方法可以自由组合。一个委托的对象可以绑定多个方法,而且这多个方法是没有限制可以任意组合的,委托灵活的绑定和解绑定策略使得使用非常方便。</p>
</blockquote>
<p><strong>类可能需要该方法的多个实现时</strong></p>
<blockquote>
<p>一个委托的对象可以绑定多个方法(<strong>多播委托</strong>),当我们运行时需要的不是单一的方法时,接口很难实现。</p>
</blockquote>
<h2 id="section-4">委托使用步骤:</h2>
<h3 id="section-5">1、定义委托</h3>
<pre><code class="language-c">
</code></pre>
<h3 id="section-6">2、实例化委托</h3>
<pre><code class="language-c#">
</code></pre>
<h3 id="section-7">3、调用(执行)委托</h3>
<pre><code class="language-c#">
</code></pre>
</body>

@ -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#
```

@ -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);
}
}
}

@ -12,4 +12,8 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Study.DelegateSeries.Core\Study.DelegateSeries.Core.csproj" />
</ItemGroup>
</Project>

Loading…
Cancel
Save