问题由来:
有一功能P1,由类A完成。现需要将功能P1进行扩展,扩展后的功能为P,其中P由原有功能P1与新功能P2组成。新功能P由类A的子类B来完成,则子类B在完成新功能P2的同时有可能会导致原有功能P1发生故障。
例子:
两数相减的功能,由类A完成:
class Program
{
static void Main(string[] args)
{
A a = new A();
Console .WriteLine("100-50=" + a.func1(100, 50));
Console .WriteLine ("100-80=" + a.func1(100, 80));
Console.Read();
}
}
class A
{
public int func1(int a, int b)
{
return a - b;
}
}
<