当前位置:网站首页>.net中 接口可以有默认实现了
.net中 接口可以有默认实现了
2022-07-06 20:23:00 【望天hous】
从 .NET Core 3.0 上的 C# 8.0 开始,可以在声明接口成员时定义实现。 最常见的方案是安全地将成员添加到已经由无数客户端发布并使用的接口。
示例:
internal interface ICustomer
{
IEnumerable<IOrder> PreviousOrders {
get; }
DateTime DateJoined {
get; }
DateTime? LastOrder {
get; }
string Name {
get; }
IDictionary<DateTime, string> Reminders {
get; }
}
internal interface IOrder
{
DateTime Purchased {
get; }
decimal Cost {
get; }
}
C# 中比较常见的两个接口。 然后新特性来了。
internal interface ICustomer
{
IEnumerable<IOrder> PreviousOrders {
get; }
DateTime DateJoined {
get; }
DateTime? LastOrder {
get; }
string Name {
get; }
IDictionary<DateTime, string> Reminders {
get; }
// Version 1:
public decimal ComputeLoyaltyDiscountVersionOne()
{
DateTime TwoYearsAgo = DateTime.Now.AddYears(-2);
if ((DateJoined < TwoYearsAgo) && (PreviousOrders.Count() > 10))
{
return 0.10m;
}
return 0;
}
// Version 2:
public static void SetLoyaltyThresholds(
TimeSpan ago,
int minimumOrders = 10,
decimal percentageDiscount = 0.10m)
{
length = ago;
orderCount = minimumOrders;
discountPercent = percentageDiscount;
}
private static TimeSpan length = new TimeSpan(365 * 2, 0, 0, 0); // two years
private static int orderCount = 10;
private static decimal discountPercent = 0.10m;
public decimal ComputeLoyaltyDiscountVersionTwo()
{
DateTime start = DateTime.Now - length;
if ((DateJoined < start) && (PreviousOrders.Count() > orderCount))
{
return discountPercent;
}
return 0;
}
//另外一种方式
public decimal ComputeLoyaltyDiscount() => DefaultLoyaltyDiscount(this);
protected static decimal DefaultLoyaltyDiscount(ICustomer c)
{
DateTime start = DateTime.Now - length;
if ((c.DateJoined < start) && (c.PreviousOrders.Count() > orderCount))
{
return discountPercent;
}
return 0;
}
}```
边栏推荐
- 树莓派设置静态ip
- Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application
- 密码学系列之:在线证书状态协议OCSP详解
- 装饰设计企业网站管理系统源码(含手机版源码)
- Ubuntu20 installation redisjson record
- Lavel PHP artisan automatically generates a complete set of model+migrate+controller commands
- 源代码保密的意义和措施
- How to customize the shortcut key for latex to stop running
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- 哈夫曼树基本概念
猜你喜欢
![Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]](/img/ce/baa4acb1b4bfc19ccf8982e1e320b2.png)
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]

杰理之播内置 flash 提示音控制播放暂停【篇】

Code quality management

哈夫曼树基本概念

How to customize the shortcut key for latex to stop running

The solution of unable to create servlet file after idea restart

How to replace the backbone of the model

【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程

Flutter3.0, the applet is not only run across mobile applications

Significance and measures of source code confidentiality
随机推荐
【达梦数据库】备份恢复后要执行两个sql语句
LAB1配置脚本
HDU ACM 4578 Transformation-&gt; Segment tree - interval change
函数重入、函数重载、函数重写自己理解
[colmap] 3D reconstruction with known camera pose
Open3D 网格滤波
Can the applet run in its own app and realize live broadcast and connection?
Experience design details
sshd[12282]: fatal: matching cipher is not supported: aes256- [email protected] [preauth]
CMB's written test - quantitative relationship
Open3d mesh filtering
Graphical tools package yolov5 and generate executable files exe
Hazel engine learning (V)
Jerry's FM mode mono or stereo selection setting [chapter]
华为小米互“抄作业”
The latest 2022 review of "small sample deep learning image recognition"
从0开始创建小程序
应用程序启动速度的优化
Opencv environment, and open a local PC camera.
【达梦数据库】添加自动收集统计信息的任务