Switch(Case)总是错的吗? [英] Is Switch (Case) always wrong?

查看:21
本文介绍了Switch(Case)总是错的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下 switch(case) 是一个很好的设计选择(除了简单性)而不是策略或类似模式......

Are there instances where switch(case) is is a good design choice (except for simplicity) over strategy or similar patterns...

推荐答案

在测试原语的时使用开关.(即整数或字符).

Use Switches when you're testing on values of primitives. (ie. integers or characters).

在不同的类型之间进行选择时使用多态性.

Use polymorphism when you are choosing between different types.

例子:测试用户输入的字符是否是a"、b"或c"之一是开关的工作.

Examples : Testing whether a character the user has entered is one of 'a', 'b' or 'c' is a job for a switch.

测试你正在处理的对象是狗还是猫是多态调度的工作.

Testing whether the object you're dealing with is a Dog or Cat is a job for polymorphic dispatch.

在许多语言中,如果您有更复杂的值,您可能无论如何都无法使用 Switch.

In many languages, if you have more complicated values you may not be able to use Switch anyway.

这篇关于Switch(Case)总是错的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆