我正在为我的一个类做一个任务,在其中,我必须使用Java语法给出静态和动态绑定的示例.
我理解基本概念,即静态绑定在编译时发生,动态绑定在运行时发生,但我无法弄清楚它们实际上是如何工作的.
我在网上找到了一个静态绑定的例子,给出了这个例子:
public static void callEat(Animal animal) {
System.out.println("Animal is eating");
}
public static void callEat(Dog dog) {
System.out.println("Dog is eating");
}
public static void main(String args[])
{
Animal a = new Dog();
callEat(a);
}
Run Code Online (Sandbox Code Playgroud)
并且这将打印"动物正在吃"因为调用callEat使用静态绑定,但我不确定为什么这被认为是静态绑定.
到目前为止,我所看到的所有来源都没有设法以我能够遵循的方式解释这一点.