您的位置:首页 > 其它

弱类型、强类型、动态类型、静态类型语言的区别是什么?

2018-03-20 10:37 423 查看
强类型:偏向于不容忍隐式类型转换。譬如说haskell的int就不能变成doubl

弱类型:偏向于容忍隐式类型转换。譬如说C语言的int可以变成double

静态类型:编译的时候就知道每一个变量的类型,因为类型错误而不能做的事情是语法错误。

动态类型:编译的时候不知道每一个变量的类型,因为类型错误而不能做的事情是运行时错误。譬如说你不能对一个数字a写a[10]当数组用。



弱类型:

> "1"+2
'12'


强类型:

>>> "1"+2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects


动态类型:

>>> a = 1
>>> type(a)
<type 'int'>
>>> a = "s"
>>> type(a)
<type 'str'>


静态类型:

Prelude> let a = "123" :: Int

<interactive>:2:9:
Couldn't match expected type `Int' with actual type `[Char]'
In the expression: "123" :: Int
In an equation for `a': a = "123" :: Int
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐