Работает ли подстановка Лискова с абстрактным классом?
Is Liskov Substitution works with out abstract class? For ex: public class SumCalculator { protected readonly int[] _numbers; public SumCalculator(int[] numbers) { _numbers = numbers; } public virtual int Calculate() => _numbers.Sum(); } public class EvenNumbersSumCalculator: SumCalculator { public EvenNumbersSumCalculator(int[] numbers) :base(numbers) { } public override int Calculate() => _numbers.Where(x => x % 2 == 0).Sum(); } When i use following statement, i get expected answer and logic is perfect. SumCalculator evenSum = new EvenNumbersSumCalculator(numbers); Console.WriteLine($"The sum of all the even numbers: {evenSum.Calculate()}"); But LSP is not followed here because when child class is substituted, behaviour got changed. Then i created an abstarct class public abstract class Calculator { protected readonly int[] _numbers; public Calculator(int[] numbers) { _numbers = numbers; } public abstract int Calculate(); } Now my both classes will implement from abstract Calculator class and i got expected result also. But my question is when we use just base class and child class, always it will changes behaviour and it will be against LSP. Is this correct? If not can somebody provide me some more details. Please dont send me links because i have seen manylinks and could not get answer.
Что я уже пробовал:
For ex: public class SumCalculator { protected readonly int[] _numbers; public SumCalculator(int[] numbers) { _numbers = numbers; } public virtual int Calculate() => _numbers.Sum(); } public class EvenNumbersSumCalculator: SumCalculator { public EvenNumbersSumCalculator(int[] numbers) :base(numbers) { } public override int Calculate() => _numbers.Where(x => x % 2 == 0).Sum(); }