关于Java中的静态和动态绑定 [英] Regarding static and dynamic binding in Java

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

问题描述

请解释Java中静态和动态绑定的概念。

Please explain the concept of static and dynamic binding in Java.

我所掌握的是Java中的静态绑定发生在编译期间,而动态绑定发生在运行时,static binding使用Type(Java中的Class)信息进行绑定,而动态绑定使用Object来解析绑定。

What I have grasped is that Static binding in Java occurs during compile time while dynamic binding occurs during Runtime, static binding uses Type (Class in Java) information for binding while dynamic binding uses Object to resolve binding.

这是我理解的代码。

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

并且上述程序的输出在集合内排序方法

and the output of the above program was inside the collection sort method

用于动态绑定...

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

输出位于Car的start方法内。请告知:这种理解是否正确,请告知更多示例。谢谢。

the output was inside the start method of Car. Please advise: Is this understanding correct and please advise some more examples. Thanks.

推荐答案

我认为您已经正确地对其进行了总结,并且假货也正确地为您添加了更多信息。为了向您添加更多信息,首先让我退一步说明方法定义与方法调用的关联称为绑定。因此,正确指出的静态绑定是编译器在编译时可以解析的绑定(也称为早期绑定或静态绑定)。另一方面,动态出价或后期绑定意味着编译器无法在编译时解析调用/绑定(它在运行时发生)。请参阅下面的示例:

I think you have summarized it correctly and shams also correctly added more information for you. Just to add little more information for you first let me step back by stating that the association of method definition to the method call is known as binding. So, static binding as you pointed out correctly, is the binding that can be resolved at compile time by compiler (also known as early binding or static binding). On the other hand, dynamic bidding or late binding it means compiler is not able to resolve the call/binding at compile time (it happens at the run time). See below for some examples:

//static binding example
class Human{
....
}
class Boy extends Human{
   public void walk(){
      System.out.println("Boy walks");
   }
   public static void main( String args[]) {
      Boy obj1 = new Boy();
      obj1.walk();
   }
}  

//Overriding is a perfect example of Dynamic binding
package beginnersbook.com;
class Human{
   public void walk()
   {
       System.out.println("Human walks");
   }
}
class Boy extends Human{
   public void walk(){
       System.out.println("Boy walks");
   }
   public static void main( String args[]) {
       //Reference is of parent class
       Human myobj = new Boy();
       myobj.walk();
   }
}

source

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

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