本来想只有抽象类有的问题,现在有点问题了。我想的答案是As A Bs B
编译器给的是As B Bs B
也就是说父类的 Send() 方法 被子类覆盖了,可是在C++中只要有定义且不是纯虚函数就没问题,能不能大虾帮我解释下,小弟哪里理解错了好么??public class regex {
public static void main(String[] args) {
B b = new B();
}
}class Printer {
public Printer(String load) {
System.out.println(load);
}
}class A {
private Printer p = new Printer("As"); public A() {
this.Send();
} public void Send() {
System.out.println("A");
}
}

解决方案 »

  1.   

    你的 B 的定义在哪里不过在java里面除了private以及final的方法外其余的方法都是动态绑定的,也就是说都是类似于C++的纯虚函数
      

  2.   

    public class regex {
    public static void main(String[] args) {
    B b = new B();
    }
    }class Printer {
    public Printer(String load) {
    System.out.println(load);
    }
    }class A {
    private Printer p = new Printer("As"); public A() {
    this.Send();
    } public void Send() {
    System.out.println("A");
    }
    }class B extends A {
    private Printer p = new Printer("Bs"); public B() {
    this.Send();
    } public void Send() {
    System.out.println("B");
    }
    }不好意思,好象漏贴了
    都是动态绑定??俄,那编程时候要小心了...
      

  3.   

    把程序改成这样就可以了
    public class regex {
    public static void main(String[] args) {
    B b = new B();

    }
    }class Printer {
    public Printer(String load) {
    System.out.println(load);
    }
    }class A {
    public A() { } public void Send() {
    System.out.println("A");
    }
    }class B extends A {
    private Printer p1;
    private Printer p2;
    public B() {
    this.Send();
    } public void Send() {
    p1 = new Printer("As");
    super.Send();
    p2 = new Printer("Bs");
    System.out.println("B");
    }
    }