静态类型绑定与动态类型绑定 [英] Static Type Binding Vs Dynamic Type Binding

查看:60
本文介绍了静态类型绑定与动态类型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解类型绑定的含义.如果我错了,请纠正我-

I understand what is meant by type binding . Please correct me if I am wrong -

类型绑定是将声明的变量关联"到特定类型的过程.
(由编译器完成).

Type binding is the process of 'associating' a declared variable to a particular type
(Done by the compiler) .

类型绑定可以分类为-
静态类型绑定
动态类型绑定

Type binding can be classified as -
Static type binding
Dynamic type binding

静态类型绑定可以通过两种类型的声明来实现-

Static type binding can be achieved by two types of declarations -

http://sankofa.loc.edu/CHU/WEB/Courses/Cosi350/Ch4/bound.2.gif

到这里为止都很清楚.
但是现在,什么是动态类型绑定? (不是定义)我知道这意味着在运行时

Its clear till here .
But now , what is Dynamic type binding ? (not the definition) I know that it means that a variable is associated with a 'type' during run-time ,

http://sankofa.loc.edu/CHU/WEB/Courses/Cosi350/Ch4/bound.3.gif

关于它的更多信息,例如-

Any more information on it , like -

  1. 为什么动态类型绑定?
  2. 可以使用哪些编程语言作为功能?
  3. 与静态类型绑定相比,它有什么优缺点?
  1. Why Dynamic type binding ?
  2. In which programming languages is it available as a feature ?
  3. What are its advantages and disadvantages over static type binding ?

通过赋值语句指定

Specified through an assignment statement

您能提供更多信息吗?应该只用赋值语句来指定它吗?

Can you give more information on it . Should it be specified only with an assignment statement ?

推荐答案

我将尝试回答您的问题:

I will try to answer your questions:

首先最简单的它可以使用哪些编程语言作为功能?

Php,Python,Ruby,Perl,JavaScript ...

Php, Python, Ruby, Perl, JavaScript ...

什么是动态类型绑定?"

使用这种方法绑定变量类型的编程语言(例如PHP,Python,Ruby等)是通用解释语言.因此,它们没有编译器.在这些语言上,您没有指定变量类型,也没有隐式规则,因此在运行类型之前发现变量类型是不可能的".

Programming languages that use this approach to bind a variable type,like PHP, Python, Ruby and so on, are in general interpreted languages. So, they do not have a compiler. On those languages you do not specify the variable type and do not have implicity rules for this, so it's "impossible" to discover a variable type before the running type.

为什么使用动态类型绑定?

要回答这个问题,我必须谈谈这种方法的一些优点.要选择使用动态类型绑定",这取决于您的语言设计.因此,取决于您要使用哪种语言以及将在哪种情况下使用它.

To answer this question, I have to talk about some advantages of this approach. To choose use Dynamic Type Binding is a decision about your language design. So, depends of what you want for your language and of which situation it will be used.

优势

  • 编写通用代码更容易.

Ex:考虑构建一个通用的数据结构,例如Stack.您想使用它来使用各种堆栈来保留所有类型的变量,例如,用于int的堆栈,用于字符串的另一个堆栈等等.要在具有静态类型绑定的**编程语言上执行此操作,将更加困难.

Ex: Think about build a generic data structure, for instance Stack. You want to use it to keep all kind of variables using different stacks, for instance, a stack for int , another for string and so on. To do this on ** programming languages with static type binding** it is more hard.

例如在C语言中,您有两个选择:

In C for instance you have two options:

  • 为每种要使用的类型写一个堆栈,请保持堆栈状态
  • 构建一个保留(void *)的堆栈,换句话说,就是所有内容的指向点

如果尝试在Python中执行此操作,则只能一次编写代码.语言会为您完成艰苦的工作.

If you try to do this in Python, you only write your code at once. The language does the hard work for you.

您可以争论Java和其他语言,但是必须记住,动态类型绑定的概念比面向对象编程还早,所以那时还没有多态性.

You can argue about Java and other languages, but you have to keep in mind that, dynamic type binding is concept older than Oriented Object Programming, so on that time, there was not Polymorphism.

缺点

  • 检查类型和解释的高费用

使用由编译生成的二进制文件执行程序比在解释器上方运行的同一程序快得多.第二件事是,当您使用静态类型绑定时,基本上,绑定过程仅在执行之前完成一次.换句话说,在具有动态类型绑定的语言中>,对每个变量的每个赋值操作都完成了绑定过程.

Execute a program using a binary file generated by compilation is much more faster than the same program running above a interpreter. The second thing is, when you are using static type binding, "basically, the binding process is done just once, before execution. In other hand, in languages with Dynamic type binding, the binding process is done o each assignment operation of each variable.

其他最小的缺点是编译器可以找到一些错误,但是使用动态类型绑定时,解释器无法找到.

Other minimal disadvantages is about some erros that a compiler could find, but using dynamic type binding the interpreter can not.

例如:由于您可以在执行期间根据需要多次更改变量的类型,因此确实有可能造成混淆,例如将int变量更改为string,然后在某些时候尝试调用a接收一个int的函数,然后使用现在是字符串的变量调用此函数.

For example: As you can change the type of your variables as many time as you need during execution time, is really possible make a confusion, like change a variable of int to string, and some point after, try to call a function that receives a int and you call this function using the variable that now is string.

这是一个最小的问题,因为经验丰富的开发人员永远不会这样做,但是仍然可以实现,而对于具有静态类型绑定的语言则不是.

This is a minimal problem, because an experient developer will never do this, but is still possible and in languages with static type binding is not.

您必须记住,我们在谈论概念,并且这些概念的实现正在与计算机科学一起发展,因此,如今可能会最大程度地减少某些优点/缺点.

You have to keep in mind that we are talking about concepts, and implementations of those concepts are evolving together with computer science, so probably some Advantages/Disadvantages can be minimized on nowadays.

这篇关于静态类型绑定与动态类型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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