我使用interface
完成了演示实现class Program
{
static void Main(string[] args)
{
#region Calling method without interface
GreaterThanZeroTest objGreaterThanZeroTest = new GreaterThanZeroTest { SomeTestVariable = 10 };
Console.WriteLine(objGreaterThanZeroTest.SomeTestMethod().ToString());
LessThanZeroTest objLessThanZeroTest = new LessThanZeroTest { SomeTestVariable = -1 };
Console.WriteLine(objLessThanZeroTest.SomeTestMethod().ToString());
#endregion
#region Calling using interface
runTest(new GreaterThanZeroTest() { SomeTestVariable = 10 });
runTest(new LessThanZeroTest() { SomeTestVariable = 10 });
#endregion
Console.ReadKey();
}
public static bool runTest(ITest test)
{
return test.SomeTestMethod();
}
}
public interface ITest
{
int SomeTestVariable { get; set; }
bool SomeTestMethod();
}
// Determines whether an int is greater than zero
public class GreaterThanZeroTest : ITest
{
public int SomeTestVariable { get; set; }
public bool SomeTestMethod()
{
return SomeTestVariable > 0;
}
}
// Determines whether an int is less than zero
public class LessThanZeroTest : ITest
{
public int SomeTestVariable { get; set; }
public bool SomeTestMethod()
{
return SomeTestVariable < 0;
}
}
我看到上述实现有两个好处:
我们将从此类实施中获得哪些其他好处以及何时应考虑应用程序架构中的接口?
答案 0 :(得分:0)
宇宙中有很多原因你可以搜索它,但这些是我认为重要的