在编程语言中,推断类型和动态类型之间有什么区别?我知道动态类型,但是不知道动态类型与推断类型有何不同以及如何?有人可以举例说明吗?
推断的类型=设置一次并在编译时设置。实际上,推断的部分只是节省时间,因为如果编译器可以找出类型名,则不必键入Typename。
类型推断通常与静态类型结合使用(与swift一样)(http://en.wikipedia.org/wiki/Type_inference)
动态类型=无固定类型->类型可以在运行时更改
静态和推断的示例:
var i = true; //compiler can infer that i most be of type Bool
i = "asdasdad" //invalid because compiler already inferred i is an Bool!
它等于
var i: bool = true; //You say i is of type Bool
i = "asdasdad" //invalid because compiler already knows i is a Bool!
==>类型推断可以节省编译器看到的类型
但如果它是动态的,则可以工作(例如objC),因为类型仅基于运行时的内容
id i = @YES; //NSNumber
i = @"lalala"; //NSString
i = @[@1] //NSArray
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句