问题标签 [static-binding]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 避免在使用层次结构参数的操作中进行静态绑定
我发现了一个关于静态绑定的问题。
我的真实课非常丰富,所以我会用几个玩具课来表达我的问题。
我们假设我们有以下层次结构。
我有一个使用层次结构定义Stock
的不同Element
专业化的类。Element
最后,我有一个StockManager
允许管理Stock
.
当然,这段代码不会编译,因为Stock
没有定义包含Element
as 参数的方法。在这种情况下,我们可以使用不同的方法修复代码。
首先,我将能够定义一个将 a 定义Element
为输入 arg 的方法,例如
这个方法可以定义一个开关来管理不同的具体元素(Element1
, Element2
)。但是,这对我来说是不可能的,因为开关违反了打开/关闭原则,而且我有很多(很多)具体元素。
另一种选择,我可以使用类似Visitor Pattern的东西。我可以将Stock
对象发送到具体元素。具体元素将负责使用Stock
操作。因此,该类可以更改为:
和StockManager
.
它允许在编译时确定具体元素的静态类型。并且动态绑定将负责调用stockOperation
正确的具体元素(Element1
或Element2
)的方法。但是!!,我在具体元素中有重复的代码,我将有几个具体的元素。
所以,我想知道我们是否知道任何模式设计或解决这个问题的最佳实践。(另一种选择,它可能使用反射,但我不想使用它)。
c++ - 我应该怎么做才能看到静态和动态绑定在起作用?[C++]
我在 Linux 上使用 GCC。
我想了解工作中的虚拟功能。
我应该编写什么样的 C++ 代码来查看和理解使用和不使用虚拟函数时静态和动态绑定是如何发生的?
以及如何“看到”它们最终是如何被绑定的,以及在这个过程中究竟发生了什么?
c++ - Is there a template that can generate static / dynamically bound versions of a class?
I'm working on some library code, and I want users to be able to take advantage of static binding if they are able to. If they are unable to instantiate a class at compile time, I want there to be a dynamic version of the class, so that it can be instantiated at run-time.
For a quick example, say I have a structure template A:
These definitions allows users of the library to instantiate A statically, and dynamically:
The problem with this method is that I have to write the definition of the class twice. I can think of a few ways of implementing a template that would generate both versions myself, but I can see it becoming a lot of work. Especially for classes that would use varying numbers of parameters, for example:
Is there a name for this pattern / problem? Is there a meta programming library which has templates which can generate both definitions for me? How do I avoid having to write the definitions of my classes twice?
python - 如何根据参数类型执行不同的操作
在像 java 这样使用静态绑定的语言中,您可以定义多个具有相同名称但参数不同的函数。学习 Python,直到现在我认为缺乏这个主要是“安全问题”(bool_parameter="False"
可能被解释为True
因为引号)。我想我只需要更加小心。
现在我发现了一种情况,缺少静态绑定简直不方便。请考虑这个元组:
要从var
静态绑定中删除项目,可以执行以下操作(伪代码:
我觉得这很方便,因为不需要条件来选择要执行的正确操作。此外,此代码使重载更容易,因为可以决定只重载其中一个函数或两者。
试图在 Python 中处理这样的情况,我只发现一些不方便的解决方案,比如一些检查类型的 if 子句。
有没有更好的办法?
c++ - Why and how can an object file of old code use new code that uses the generic programming paradigm even though templates are static binding?
This is an entirely different question than the one I asked before which is why I'm posting this.
I would like to define my topic to be a subjective question that inspires answers which explain "why" and "how". This is allowed according to the Help Center rules.
In order to make my question more constructive, I provide you with resources that better explain my topic.
From the C++ SuperFAQ under the question
"Can you give me a simple reason why virtual functions (dynamic binding, dynamic polymorphism) and templates (static polymorphism) make a big difference?"
The author says, "...a programmer might write some code that is called by a framework that was written by their great, great grandfather. There’s no need to change great-great-grandpa’s code. In fact, for dynamic binding with virtual functions, it doesn’t even need to be recompiled.Even if all you have left is the object file and the source code that great-great-grandpa wrote was lost 25 years ago, that ancient object file will call the new extension without anything falling apart."
He goes on to say, "That is extensibility, and that is OO and generic programming for powerful reusable abstraction."
Furthermore, I recently read a paper written by the creator of C++ Bjarne Stroustrup called, "Why C++ is not just an Object-Oriented Programming Language". In his paper, he defines a language or technique as object-oriented if and only if it directly supports:
Abstraction – providing some form of classes and objects.
Inheritance – providing the ability to build new abstractions out of existing ones.
Run-time polymorphism – providing some form of run-time binding.
He also briefly mentions generic programming.
6.7 Generic Programming - A major theme in the C ++ community over the last few years has been the development of techniques exploiting the template mechanism.
From reading these two resources I can understand how old code is able to use new code by means of the object-oriented paradigm. However, I do not understand how old code can use new code by use of generic programming (in this case templates) because generic programming uses static binding. From my previous related question Ben Voigt commented that,
"For "old" template code to be combined with "new" template code, it is necessary for both to be compiled together."
The C++ SuperFAQ seems to imply (for both OO and generic programming) that the old code should not have to be recompiled with the new code in order to use the new code, and that you should just need the object file from the old code. Thereby, maintaining code reusability.
Could someone please answer "why" and "how" an object file of old code can use new code that uses the generic programming paradigm even though templates are static binding?
Edit
I would like to explain the answer below in a little more detail since it will help me understand more and may help others understand. Since generic programming uses static binding, both the source code of the old code and that of the new code must be re-compiled "together" for code reusability to take effect. This means, if you only have the object file of the old code, you would not be able to use the new code with it since that would require dynamic/late binding which is binding at run-time.
java - 案例:静态绑定?动态绑定?
我知道重载使用静态绑定,而覆盖使用动态绑定。但如果它们混合在一起呢?根据本教程,为了解决方法调用,静态绑定使用类型信息,而动态绑定使用实际的对象信息。
那么,下面的例子中是否发生了静态绑定来确定sort()
调用哪个方法呢?
.
.
ps:输出为:
Child#sort(Collection c) is invoked
c++ - 在 const 和非 const 引用变量的情况下,何时决定调用哪个函数?
考虑以下函数
在这种情况下,当函数调用与函数定义绑定时,它是在编译时还是运行时,因为一个是左值i
而另一个不是?
除此之外,这两个函数在参数的数量和类型方面是相同的。
java - Java中的绑定(覆盖方法和“字段”)
B类是A类的子类,C类是B类的子类。
为什么在
P1
从类型调用 1 中,Print1
即使它引用类型的对象,Print2
而在调用 2 中它的行为是对Print2
-object 的引用?为什么在 Call 2
print(B b)
中被调用Print2
而不是print(C c)
?
到目前为止,这对我来说是 Java 中最令人困惑的事情。谢谢您的帮助。
java - 子类型多态性:总是后期绑定?
该维基百科指出:
由于在运行前(通常)不知道多态对象的具体类型,因此执行的函数是动态绑定的。以下面的 Java 代码为例:
List 是一个接口,所以 list 必须引用它的子类型。它是对 LinkedList、ArrayList 还是其他 List 子类型的引用?add 引用的实际方法直到运行时才知道。
考虑这个例子:
为什么这里引用的实际方法直到运行时才知道?编译器不能只检查对象列表分配给哪种类型的 foo 的每个调用吗?当然,这只有在程序是确定性的并且不涉及随机性(例如用户交互)时才有可能。
这是引用声明中的(一般)是关于还是我的理解错误?
在程序是确定性的特殊情况下,是使用静态绑定还是 - 在 Java 中 - 总是使用动态绑定,不管有什么可能?如果是这样,为什么?
c++ - C++ 静态和动态绑定行为
我理解静态绑定和动态绑定之间的区别,因为静态绑定的方法调用是在编译时确定的,而动态绑定的方法调用是在运行时确定的。
我不明白的一件事是为什么您必须通过引用或指针传递动态绑定。我尝试在网上查找,但我仍然感到困惑。是不是因为当你按值传递时,你传递的是一个副本,这意味着它必须被初始化,这意味着它被切片?
例如,Pet
是基类,又Dog
是派生类。
现在...
如果有人能更好地向我解释这一点,那真的会让我开心
谢谢