动态绑定:是很重要的特性,它能使程序变得可扩展而不需要重编译已存代码为什么?能不能具体给我讲讲,举个例子之类的

解决方案 »

  1.   

    动态绑定是指“在执行期间”这里说的不是编译时候 判断所引用对象的实际类型,根据其实际的类型调用其相应的方法(实际调用的就是你NEW的那个)我记得THINKING IN JAVA有个很经典的例子 你可以去看看
      

  2.   

    class Shape { 
      void draw() {}
      void erase() {} 
    }class Circle extends Shape {
      void draw() { 
        System.out.println("Circle.draw()"); 
      }
      void erase() { 
        System.out.println("Circle.erase()"); 
      }
    }class Square extends Shape {
      void draw() { 
        System.out.println("Square.draw()"); 
      }
      void erase() { 
        System.out.println("Square.erase()"); 
      }
    }class Triangle extends Shape {
      void draw() { 
        System.out.println("Triangle.draw()"); 
      }
      void erase() { 
        System.out.println("Triangle.erase()");
      }
    }public class Shapes {
      public static Shape randShape() {
        switch((int)(Math.random() * 3)) {
          default: // To quiet the compiler
          case 0: return new Circle();
          case 1: return new Square();
          case 2: return new Triangle();
        }
      }
      public static void main(String[] args) {
        Shape[] s = new Shape[9];
        // Fill up the array with shapes:
        for(int i = 0; i < s.length; i++)
          s[i] = randShape();
        // Make polymorphic method calls:
        for(int i = 0; i < s.length; i++)
          s[i].draw();
      }
    } ///:~此例子便是动态绑定的例子。。好好看看吧thinking in Java里的