Dmi*_* Kh 2 c# oop liskov-substitution-principle
我一直在寻找代码中的迹象,表明可能会违反里氏替换原则。首先我创建了一个简单的类和另一个继承它的类:
public class Adder
{
public virtual int Add(int operand1, int operand2)
{
return operand1 + operand2;
}
}
public class AdvancedAdder : Adder
{
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个检测 LSP 违规的 UnitTest:
public abstract class AdderUnitTestsBase
{
protected static Adder Adder;
[DataTestMethod]
[DataRow(2, 2, 4)]
[DataRow(-1, 2, 1)]
[DataRow(2, -3, -1)]
[DataRow(0, 0, 0)]
public void Add_ReturnsCorrectResult(
int operand1, int operand2, int result)
{
Assert.AreEqual(result, Adder.Add(operand1, operand2));
}
}
[TestClass]
public class AdderUnitTests : AdderUnitTestsBase
{
[ClassInitialize]
public static void ClassInit(TestContext context)
{
Adder = new Adder();
}
}
[TestClass]
public class AdvancedAdderUnitTests : AdderUnitTestsBase
{
[ClassInitialize]
public static void ClassInit(TestContext context)
{
Adder = new AdvancedAdder();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试了不同的事情,例如更改参数类型或更改参数数量:
public class AdvancedAdder : Adder
{
public int Add(int operand1, int operand2, int operand3)
{
return operand1 + operand2 + operand3;
}
public uint Add(uint operand1, uint operand2)
{
return operand1 + operand2;
}
}
Run Code Online (Sandbox Code Playgroud)
它没有违反 LSP,因为这些方法具有不同的签名。它只是扩展了功能。唯一有效的是使用“override”关键字:
public class AdvancedAdder : Adder
{
public override int Add(int operand1, int operand2)
{
return operand1 - operand2;
}
}
Run Code Online (Sandbox Code Playgroud)
看起来如果我在 C# 中避免“覆盖”,我就不需要担心可能违反里氏替换原则。你认为这是正确的吗?
重写只是重新定义方法的一种技术方案。LSP 更多的是关于语义而不是技术。
看起来如果我在 C# 中避免“覆盖”,我就不需要担心可能违反里氏替换原则。
也许您的意思是多态性,因为override它只是改变对象行为的一种方法(动态绑定)。您还可以用于new非virtual方法(静态绑定)。您甚至可以使用检查(作为反射的特殊情况)并基于此更改行为:
public void method()
{
switch (this.getType().Name)
{
case "Square":
// do the square thing
break;
case "Rectangle":
// do the rectangle thing
break;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样定义的方法可以很好地破坏 LSP,而无需继承或override/new关键字。
它确实比普通语法更复杂,并且更多地涉及语义(含义)。
即使是带有正方形/矩形的经典 LSP 示例也都是关于含义的。如果您认为正方形/矩形只是没有语义基础的单词,那么您对它们的行为就没有期望。因此,您没有理由认为可以用其中一种替代另一种并且无法破坏 LSP。
另一方面,继承是一种代码构造,它告诉您:
PARENT
^
|
CHILD
Run Code Online (Sandbox Code Playgroud)
孩子可以替代父母,因为它继承了所有行为。
| 归档时间: |
|
| 查看次数: |
647 次 |
| 最近记录: |