1. 静态类型(Statically Typed):A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and C are statically typed languages.
2. 动态类型(Dynamically Typed):A language in which types are discovered at execution time; the opposite of statically typed. VBScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value.
3. 强类型(Explicit Typed):A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you can't treat it like a string without explicitly converting it.
4. 弱类型(Implicit Typed):A language in which types may be ignored; the opposite of strongly typed. VBScript is weakly typed. In VBScript, you can concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion.
下边再说下一个与语言类型相关的概念叫鸭子类型(Duck Typing),什么是鸭子类型呢?
“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子。
“ In other words, don't check whether it IS-a duck: check whether it QUACKS-like-a duck, WALKS-like-a duck, etc, depending on exactly what subset of duck-like behaviour you need to play your language-games with.”换句话说,不管它是不是鸭子,而要看它像不像鸭子,取决于它到底有没有需要的鸭子一样的行为子集。即便它是一只学鸭叫鸭走鸭游的变态鸡也可以称它鸭子,说白了它默许不同类型的对象出现在同一位置,像多态一样工作却没有继承。
鸭子类型是动态类型和某些静态语言的一种对象推断风格,本身不关注对象的类型,而在乎对象的行为(对象的方法),如果对象调用的方法不存在,将引发一个运行时错误。所以说上文讲的相似性没有明确的边界,到底多像才能称鸭子其实不是一个问题,甚至可能只要你能叫能跑走能游就成,具体怎么叫怎么走怎么游都无所谓。因为猪是会叫会走还能游的,所以在某些场景下猪也可被称为鸭子。
转载于:https://www.cnblogs.com/XiongMaoMengNan/p/7604013.html
原文链接:https://blog.csdn.net/weixin_30342827/article/details/98046357
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~