当前位置:网站首页>.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;
}
}```
边栏推荐
- 从0开始创建小程序
- 如何自定义Latex停止运行的快捷键
- 25.(arcgis api for js篇)arcgis api for js线修改线编辑(SketchViewModel)
- [Dameng database] after backup and recovery, two SQL statements should be executed
- Flink Task退出流程与Failover机制
- Jerry's FM mode mono or stereo selection setting [chapter]
- Flink task exit process and failover mechanism
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- 【达梦数据库】备份恢复后要执行两个sql语句
- Open3d mesh filtering
猜你喜欢
![[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial](/img/58/d869939157669891f369fb274d32af.jpg)
[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial

“去虚向实”大潮下,百度智能云向实而生

Flink task exit process and failover mechanism

变量、流程控制与游标(MySQL)

When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks

Experience design details

VHDL实现任意大小矩阵乘法运算

The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing

Huawei and Xiaomi "copy each other"

Flutter3.0了,小程序不止于移动应用跨端运行
随机推荐
[untitled]
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
Leetcode-02 (linked list question)
The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing
Ubuntu 20 installation des enregistrements redisjson
从0开始创建小程序
【colmap】已知相机位姿情况下进行三维重建
Jerry's RTC clock development [chapter]
CMB's written test - quantitative relationship
Code quality management
Domcontentloaded and window onload
Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
HDU ACM 4578 Transformation-&gt;段树-间隔的变化
The version control of 2021 version is missing. Handling method
【达梦数据库】添加自动收集统计信息的任务
input_delay
Jerry's ble exiting Bluetooth mode card machine [chapter]
1200.Minimum Absolute Difference
【达梦数据库】备份恢复后要执行两个sql语句
Set static IP for raspberry pie