违反面向对象的概念?

Violation of OO concepts?

Create a base class with a method that uses a switch case that reference the type of the children class.

下面这段代码是否违反了面向对象的原则,如果是的话是哪一个?

public abstract class BaseClass
{
    public BaseClass Method()
    {
        switch (this)
        {
            case DerivedClass1 s:
                return new DerivedClass1();
            case DerivedClass2 c:
                return new DerivedClass2();
            case DerivedClass3 r:
                return new DerivedClass3();
            default:
                return null;
        }
    }
}

你违反了 open-closed principle of SOLID by not taking advantage of polymorphism. Make the base class method virtual and override 它的每个派生 class:

public abstract class BaseClass
{
    public virtual BaseClass Method()
    {
        return null;
    }
}

public class DerivedClass1 : BaseClass
{
    public override BaseClass Method()
    {
        return new DerivedClass1();
    }
}

...等等。