在java里重载和覆盖有什么区别吗??
能不能给个例程??

解决方案 »

  1.   

    public Class1{
     public void method1(String a){
      ...
     }
     public void method2(String a){
      ...
     }
    }
    public Class2 extends Class1{
     public void method1(String a){     //覆盖
      ...
     }
     public void method2(int a){        //重载
      ...
     }
    }
      

  2.   

    你看看有关向上转型(upcasting)就可以了解点多态了,要直接解释比较不容易懂,找点例程看吧!
      

  3.   

    Java 中所有的函数默认都是虚函数(构造函数与静态函数除外), 可以实现多态: 如下
    class Test1{
      public static void main(String[] args){
        Shape shape = new Shape();
        shape.draw();
        
        shape = new Sqaure();
        shape.draw();
        
        shape = new Circle();
        shape.draw();
      }
    }class Shape{
       void draw(){
        System.out.println("Shape");
       }
    }class Sqaure extends Shape{
       void draw(){
        System.out.println("Sqaure");
       }
    }class Circle extends Shape{
       void draw(){
        System.out.println("Circle");
       }
    }