有人可以告诉我强打字和弱打字是什么意思,哪个更好? [英] Can someone tell me what Strong typing and weak typing means and which one is better?

查看:120
本文介绍了有人可以告诉我强打字和弱打字是什么意思,哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我强打字和弱打字是什么意思,哪个更好?

Can someone tell me what Strong typing and weak typing means and which one is better?

推荐答案

这将是理论上需要注意的答案,但实践方面似乎已经被忽略了……

That'll be the theory answers taken care of, but the practice side seems to have been neglected...

强类型化意味着您不能在期望使用另一种类型的变量中使用它(或者这样做会受到限制).弱类型意味着您可以混合使用不同的类型.例如,在PHP中,您可以混合数字和字符串,而PHP不会抱怨,因为它是一种弱类型的语言.

Strong-typing means that you can't use one type of variable where another is expected (or have restrictions to doing so). Weak-typing means you can mix different types. In PHP for example, you can mix numbers and strings and PHP won't complain because it is a weakly-typed language.

$message = "You are visitor number ".$count;

如果它是强类型的,则必须将$ count从整数转换为字符串,通常使用强制转换:

If it was strongly typed, you'd have to convert $count from an integer to a string, usually with either with casting:

$message = "you are visitor number ".(string)$count;

...或功能:

$message = "you are visitor number ".strval($count);

至于哪个更好,那是主观的.倡导强类型的人会告诉您,它将帮助您避免某些错误和/或错误,并有助于传达变量的用途等.他们还将告诉您,弱类型的拥护者将称呼强类型为"不必要的语言修饰,通过常识或类似的用法变得毫无意义.作为弱类型团队的持卡会员,我不得不说他们有我的电话号码...但是我也有他们的电话号码, I 可以将它放在一个字符串中:)

As for which is better, that's subjective. Advocates of strong-typing will tell you that it will help you to avoid some bugs and/or errors and help communicate the purpose of a variable etc. They'll also tell you that advocates of weak-typing will call strong-typing "unnecessary language fluff that is rendered pointless by common sense", or something similar. As a card-carrying member of the weak-typing group, I'd have to say that they've got my number... but I have theirs too, and I can put it in a string :)

这篇关于有人可以告诉我强打字和弱打字是什么意思,哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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