如何消除switch
使用多态来消除switch:Link.
With polymorphism, this:
foreach (var animal in zoo) { switch (typeof(animal)) { case "dog": echo animal.bark(); break; case "cat": echo animal.meow(); break; } }
becomes this:
foreach (var animal in zoo) { echo animal.speak(); }