当前位置:网站首页>.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;
}
}```
边栏推荐
- 【无标题】
- Laravel php artisan 自动生成Model+Migrate+Controller 命令大全
- Set WiFi automatic connection for raspberry pie
- 树莓派设置静态ip
- U.S. Air Force Research Laboratory, "exploring the vulnerability and robustness of deep learning systems", the latest 85 page technical report in 2022
- 20.(arcgis api for js篇)arcgis api for js面采集(SketchViewModel)
- The latest 2022 review of "small sample deep learning image recognition"
- Jerry's transmitter crashed after the receiver shut down [chapter]
- LAB1配置脚本
- 体会设计细节
猜你喜欢
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
About Confidence Intervals
22.(arcgis api for js篇)arcgis api for js圆采集(SketchViewModel)
制作(转换)ico图标
树莓派设置静态ip
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Ubuntu20 installation redisjson record
Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
RestClould ETL 社区版六月精选问答
CVPR 2022 best paper candidate | pip: six inertial sensors realize whole body dynamic capture and force estimation
随机推荐
编译常量、ClassLoader类、系统类加载器深度探析
【C语言】 题集 of Ⅸ
Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】
Don't you know the relationship between JSP and servlet?
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
Flink task exit process and failover mechanism
Stored procedures and functions (MySQL)
unrecognized selector sent to instance 0x10b34e810
Hazel engine learning (V)
“去虚向实”大潮下,百度智能云向实而生
Flutter3.0, the applet is not only run across mobile applications
[Dameng database] after backup and recovery, two SQL statements should be executed
Jerry's ble exiting Bluetooth mode card machine [chapter]
树莓派设置静态ip
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
sshd[12282]: fatal: matching cipher is not supported: aes256- [email protected] [preauth]
RestClould ETL 社区版六月精选问答
Jerry's FM mode mono or stereo selection setting [chapter]
应用程序启动速度的优化
如何替换模型的骨干网络(backbone)