C# 两个类实现了相同的接口,但一个方法在两个类之间具有相同的实现,从而导致代码重复
考虑以下场景:C# 两个类实现了相同的接口,但一个方法在两个类之间具有相同的实现,从而导致代码重复,c#,.net,interface,architecture,C#,.net,Interface,Architecture,考虑以下场景: public interface ITestInterface { void TestMethod1(); void TestMethod2(); } public class TestParent { void SomeMethod() { Console.Writeln("Method of test parent"); } } public class Test1: TestParent, ITe
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实现。为什么不使用抽象基类
public abstract class TestBase: TestParent, ITestInterface
{
void SomeMethod()
{
Console.Writeln("Method of test parent");
}
#region ITestInterface
public void TestMethod1()
{
Console.WriteLine("Implementation 1 of TestMethod1");
}
public void TestMethod2()
{
Console.log("Same implementation");
}
#endregion
}
public class Test1 : TestBase
{
}
public class Test2 : TestBase
{
}
为什么不使用抽象基类呢
public abstract class TestBase: TestParent, ITestInterface
{
void SomeMethod()
{
Console.Writeln("Method of test parent");
}
#region ITestInterface
public void TestMethod1()
{
Console.WriteLine("Implementation 1 of TestMethod1");
}
public void TestMethod2()
{
Console.log("Same implementation");
}
#endregion
}
public class Test1 : TestBase
{
}
public class Test2 : TestBase
{
}
您需要添加一个中间类TestParentExtension,它扩展TestParent并实现TestMethod2。然后,您可以为Test1和Test2而不是TestParent扩展这个中间类 给你。我为你整理了一些语法
public interface ITestInterface {
void TestMethod1();
void TestMethod2();
}
public class TestParent {
public void SomeMethod() {
Console.WriteLine("Method of test parent");
}
}
public class IntermediateParent: TestParent {
public void TestMethod2() {
Console.WriteLine("Same implementation");
}
}
public class Test1: IntermediateParent, ITestInterface {
public void TestMethod1() {
Console.WriteLine("Implementation 1 of TestMethod1");
}
}
public class Test2: IntermediateParent, ITestInterface {
public void TestMethod1() {
Console.WriteLine("Implementation 2 of TestMethod1");
}
}
您需要添加一个中间类TestParentExtension,它扩展TestParent并实现TestMethod2。然后,您可以为Test1和Test2而不是TestParent扩展这个中间类 给你。我为你整理了一些语法
public interface ITestInterface {
void TestMethod1();
void TestMethod2();
}
public class TestParent {
public void SomeMethod() {
Console.WriteLine("Method of test parent");
}
}
public class IntermediateParent: TestParent {
public void TestMethod2() {
Console.WriteLine("Same implementation");
}
}
public class Test1: IntermediateParent, ITestInterface {
public void TestMethod1() {
Console.WriteLine("Implementation 1 of TestMethod1");
}
}
public class Test2: IntermediateParent, ITestInterface {
public void TestMethod1() {
Console.WriteLine("Implementation 2 of TestMethod1");
}
}
我已经更新了我的问题,添加了更多的上下文。TestParent是一个现有的类,我需要扩展实现ITestInterface的类。@kaikadi bella:因为您提供了伪类,所以很难建议如何重构代码。例如,不可能让TestParent实现接口?或者以另一种方式,让TestBase从TestParentEdit my answer继承。TestParent无法实现该接口,因为TestParent的其他派生类实现了其他接口。添加TestBase继承自TestParent即可。谢谢我已经更新了我的问题,添加了更多的上下文。TestParent是一个现有的类,我需要扩展实现ITestInterface的类。@kaikadi bella:因为您提供了伪类,所以很难建议如何重构代码。例如,不可能让TestParent实现接口?或者以另一种方式,让TestBase从TestParentEdit my answer继承。TestParent无法实现该接口,因为TestParent的其他派生类实现了其他接口。添加TestBase继承自TestParent即可。谢谢在基类和子类之间再添加一个类。在基类和子类之间再添加一个类。谢谢,我会接受这个答案。但是,当蒂姆回答时,我遗漏了一些上下文,但他也有点暗示了同样的意思。谢谢,我会接受这个答案。但是,当蒂姆回答时,我错过了一些上下文,但他有点暗示了同样的意思。