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

查看:138
本文介绍了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.

测试您正在处理的对象是否为Dog或Cat是多态调度的工作。

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天全站免登陆