我正在阅读这篇博客,.NET Engineering Team
他们为. 我对它的动机感到困惑,而不是. 除此之外,我无法弄清楚任何其他好处。考虑以下代码:Interface
abstract class
C# 7(不可能提供方法定义)
interface ILogger
{
void LogData(dynamic data, dynamic logMode);
bool SendStatusEmail(string emailAddress);
}
public class EmployeeLog : ILogger
{
public void LogData(dynamic data, dynamic logMode)
{
throw new NotImplementedException();
}
public bool SendStatusEmail(string emailAddress)
{
throw new NotImplementedException();
}
}
public abstract class Logger
{
public abstract void LogData(dynamic data, dynamic logMode);
public bool SendStatusEmail(string emailAddress)
{
// Email Sending Code
return true;
}
}
public class EmployeeLog : Logger
{
public override void LogData(dynamic data, dynamic logMode)
{
throw new NotImplementedException();
}
}
C# 8(可以提供方法定义)
interface ILogger
{
void LogData(dynamic data, dynamic logMode);
public bool SendStatusEmail(string emailAddress)
{
// Email Sending Code
return true;
}
}
public class EmployeeLog : ILogger
{
public void LogData(dynamic data, dynamic logMode)
{
throw new NotImplementedException();
}
}
public abstract class Logger
{
public abstract void LogData(dynamic data, dynamic logMode);
public bool SendStatusEmail(string emailAddress)
{
// Email Sending Code
return true;
}
}
public class EmployeeLog : Logger
{
public override void LogData(dynamic data, dynamic logMode)
{
throw new NotImplementedException();
}
}
在C# 8
中,两者abstract class
和interface
都可以完成向其成员提供默认实现的相同工作。我对此有以下疑问:
如果Interface提供了默认实现,也支持多重继承,那么c#8中需要抽象类吗?
在这种情况下会发生什么?
:
interface ILogger
{
public bool SendStatusEmail(string emailAddress)
{
// Email Sending Code
return true;
}
}
interface IEmail
{
public bool SendStatusEmail(string emailAddress)
{
// Email Sending Code
return true;
}
}
public class EmployeeLog : ILogger, IEmail
{
}
public class Test
{
EmployeeLog emp = new EmployeeLog();
emp.SendStatusEmail(); //Which function it will refer to?
}