什么是动态类型?

发布于 2024-12-04 17:54:27 字数 42 浏览 1 评论 0原文

我听说过这个术语与 PH​​P 等脚本语言一起使用。它到底是什么意思?

I've heard this term used with scripting languages such as PHP. What exactly does it mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上得阳光 2024-12-11 17:54:27

动态类型是语言的决定性特征。一个简短的解释可能是:

当语言不关联值时,它就具有动态类型
严格与特定类型相关,但它旨在“决定”什么
值的类型应在运行时确定,具体取决于您尝试使用它的方式。

例如,在 PHP 中,您可以编写

$count = "5"; // defines a string variable

然后继续说

$count = $count * 2; // this is legal and has the obvious result¹

这里发生了什么?其一,编译器不会抱怨您试图将字符串乘以数字并拒绝编译程序(就像在 C、C++、C# 和 Java 等语言中会发生的情况)。它生成了将参数 $count2 转发到乘法运算符的代码,就像您所要求的那样并继续前进。

现在编译程序后,动态类型在运行时生效。当乘法运算符开始查看其操作数时,它会检查每个操作数的当前类型(如果您愿意)。和以前一样,它是一个字符串和一个整数。但该运算符知道它只能将两个整数相乘(为了简单起见,让我们忽略浮点数),因此它必须以某种方式从字符串中生成一个整数值。所有动态类型语言都有规定此类转换如何工作的规则所有值类型对之间;在这种情况下,PHP 生成整数 5 来自字符串“5”。

您可能会遇到的动态类型的另一个方面称为鸭子类型;这仅适用于类类型的值(即不是基元)。简而言之,鸭子类型规定,当您编写时,

$object->quack();

编译器不会尝试查看 $object 是否属于具有名为 quack 且不带参数的方法的类型。相反,它会在运行时尝试查看 $object 是否确实具有这样的方法;如果是,无论什么类型都会调用该方法
我们手头的对象(可能是一只鸭子,也可能是一只狗,编译器会关心)。

脚注:

1 将字符串乘以整数就是动态类型的全部内容(从字符串生成整数,因为乘法需要一个整数);然而,这里也存在松散类型(允许编译乘法而无需证明两个操作数实际上都是整数)。

Dynamic typing is a definitive characteristic of a language. A short explanation might be:

A language has dynamic typing when it does not associate values
strictly with a specific type, but it is designed to "decide" what
the type of a value should be at runtime, based on how you are attempting to use it.

For example, in PHP you can write

$count = "5"; // defines a string variable

and then go on to say

$count = $count * 2; // this is legal and has the obvious result¹

What happened here? For one, the compiler did not complain that you are attempting to multiply a string by a number and refuse to compile the program (such as would happen in languages like C, C++, C# and Java). It produced code to forward the arguments $count and 2 to the multiplication operator just like you asked and moved on.

With the program now compiled, dynamic typing comes into effect at runtime. When the multiplication operator gets around to look at its operands, it checks to see what is the current, if you will, type of each one. As before, it's a string and an int. But the operator knows that it can only multiply two integers (let's ignore floats for simplicity), so it has to somehow produce an integer value from the string. All dynamically typed languages have rules that stipulate how such a conversion works between all pairs of value types; in this case, PHP produces the integer 5 from the string "5".

Another aspect of dynamic typing you might come across is called duck typing; this only applies to values of class types (i.e. not primitives). In short, duck typing stipulates that when you write

$object->quack();

the compiler will not attempt to see if $object is of a type that has a method named quack that takes no arguments. Rather, it will attempt at runtime to see if $object actually has such a method; if it does, the method will be called regardless of what type
of object we have at hand (might be a duck, might be a dog for all the compiler cares).

Footnotes:

¹ Multiplying a string by an integer is what dynamic typing is all about (producing an integer from a string because the multiplication demands one); however, there is also loose typing at work here (allowing the multiplication to compile without being able to prove that both operands are actually ints).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文