静态比Java中的动态绑定 [英] Static Vs. Dynamic Binding in Java

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

问题描述

我正在为我的一个类做一个任务,在其中,我必须使用Java语法提供 static 动态绑定的示例。

I'm currently doing an assignment for one of my classes, and in it, I have to give examples, using Java syntax, of static and dynamic binding.

我理解基本概念,即静态绑定在编译时发生,动态绑定在运行时发生,但我无法弄清楚它们是如何实际工作的。

I understand the basic concept, that static binding happens at compile time and dynamic binding happens at runtime, but I can't figure out how they actually work specifically.

我在网上找到了一个静态绑定示例,给出了这个例子:

I found an example of static binding online that gives this example:

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);
}

这会打印出动物正在吃东西,因为电话到 callEat 使用静态绑定,但我不确定为什么这被认为是静态绑定。

And that this would print "animal is eating" because the call to callEat uses static binding, but I'm unsure as to why this is considered static binding.

到目前为止,我所看到的所有来源都没有设法以我可以遵循的方式解释这一点。

So far none of the sources I've seen have managed to explain this in a way that I can follow.

推荐答案

来自 Javarevisited博客文章


以下是静态和动态绑定之间的一些重要区别:

Here are a few important differences between static and dynamic binding:


  1. Java中的静态绑定在编译期间发生,而动态绑定在运行时发生。

  2. 私人最终静态方法和变量使用静态绑定,并且在运行时根据运行时对象绑定虚拟方法时由编译器绑定。

  3. 静态绑定使用键入(Java中的 class )信息进行绑定动态绑定使用object来解析绑定。

  4. 重载方法使用静态绑定绑定,而重写方法在运行时使用动态绑定绑定。

  1. Static binding in Java occurs during compile time while dynamic binding occurs during runtime.
  2. private, final and static methods and variables use static binding and are bonded by compiler while virtual methods are bonded during runtime based upon runtime object.
  3. Static binding uses Type (class in Java) information for binding while dynamic binding uses object to resolve binding.
  4. Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.

这是一个帮助您了解Java中的静态和动态绑定的示例。

Here is an example which will help you to understand both static and dynamic binding in Java.

Java中的静态绑定示例

public class StaticBindingTest {  
    public static void main(String args[]) {
        Collection c = new HashSet();
        StaticBindingTest et = new StaticBindingTest();
        et.sort(c);
    }
    //overloaded method takes Collection argument
    public Collection sort(Collection c) {
        System.out.println("Inside Collection sort method");
        return c;
    }
    //another overloaded method which takes HashSet argument which is sub class
    public Collection sort(HashSet hs) {
        System.out.println("Inside HashSet sort method");
        return hs;
    }
}

输出:内部收藏排序方法

Java动态绑定示例

public class DynamicBindingTest {   
    public static void main(String args[]) {
        Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car
        vehicle.start(); //Car's start called because start() is overridden method
    }
}

class Vehicle {
    public void start() {
        System.out.println("Inside start method of Vehicle");
    }
}

class Car extends Vehicle {
    @Override
    public void start() {
        System.out.println("Inside start method of Car");
    }
}

输出:内部开始汽车的方法

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

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