【发布时间】:2021-08-05 20:41:16
【问题描述】:
考虑以下场景:
public interface ITestInterface
{
void TestMethod1();
void TestMethod2();
}
public class TestParent
{
void SomeMethod()
{
Console.Writeln("Method of test parent");
}
}
public class Test1: TestParent, ITestInterface
{
void TestMethod1()
{
Console.WriteLine("Implementation 1 of TestMethod1");
}
void TestMethod2()
{
Console.log("Same implementation");
}
}
public class Test2: TestParent, ITestInterface
{
void TestMethod1()
{
Console.WriteLine("Implementation 2 of TestMethod1");
}
void TestMethod2()
{
Console.log("Same implementation");
}
}
TestParent 是一个现有类,Test1 和 Test2 是 TestParent 的子类并实现 ITestInterface。
在我上面的示例中,两个类都具有相同的 TestMethod2() 实现。 我只是想知道如何避免重复代码? 我计划添加更多类,它们都具有相同的 TestMethod2 实现。
【问题讨论】:
-
在基类和子类之间再添加一个类。
标签: c# .net interface architecture