Why is this switch on type case considered confusing?

前端 未结 2 1963
鱼传尺愫
鱼传尺愫 2020-11-30 15:45

I was looking for a way to refactor and simplify one function where I have to do data sorting depending on input class type, and got stuck at switch(input.GetType()):<

2条回答
  •  一向
    一向 (楼主)
    2020-11-30 16:06

    It seems you don't expect the switch to match on subclasses. But this would break the Liskov Substitution Principle. (where if you passed in a C object, the code would work, but not with a D, even though D is a subclass of C).

提交回复
热议问题