gpt4 book ai didi

java - 没有方法的静态绑定(bind)和动态绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 03:01:36 25 4
gpt4 key购买 nike

我在大学里参加了面向对象编程的考试。其中一个问题是关于静态绑定(bind)和动态绑定(bind)。

问题如下:

Shape s; if(i==1) s = new Point(1,2); else s = new Rectange(10,20); //this is dynamic binding.

是/否 enter image description here顺便说一句,这不是我的答案。

我的老师说答案是否定的,因为它是静态绑定(bind)。

据我所知,静态绑定(bind)和动态绑定(bind)仅在我调用方法时发生。我阅读了所有 StackOverflow 问题和大量关于该主题的博客文章,我唯一能想到的答案是存在动态绑定(bind)

任何解释将不胜感激。

最佳答案

“绑定(bind)”只是意味着您将一个名称与一个对象相关联,因此这里存在绑定(bind)。

这是动态绑定(bind),参见 the wikipedia article :

The binding of names before the program is run is called static (also "early"); bindings performed as the program runs are dynamic (also "late" or "virtual").

An example of a static binding is a direct C function call: the function referenced by the identifier cannot change at runtime.

But an example of dynamic binding is dynamic dispatch, as in a C++ virtual method call. Since the specific type of a polymorphic object is not known before runtime (in general), the executed function is dynamically bound.

即使发布的代码通过设置 i 预先确定了 s 的设置,但动态的原因是在 s 上调用的方法将在运行时得到解析。

关于java - 没有方法的静态绑定(bind)和动态绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34498348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com