日期:2014-05-20  浏览次数:20705 次

Java继承与多态问题.
Java code
class Instrruement{

    public void play(){

    System.out.println(”Instrruement play“);

  }

  static void tune(Instrruement i){

    i.play();

  }

}

Class Grasp{

    public void play(){

    System.out.println(”Grasp play“);

public void main(String[] args)

    Instrruement flu=new Grasp();

    Instrruement.tune(flu);

}

这个程序的输出结果是什么?为什么?

指教一下这类问题的方法

请高人指教,尽快!!!

------解决方案--------------------
输出结果为:Grasp play
原因:是叫向上兼容法则吧
------解决方案--------------------
其实这个和编译器的编写有关的
------解决方案--------------------
这个也是程序解耦的一个常用方法:
用父类/抽象类或接口来声明,用子类来实例化,这样,当实现变了,程序也不用太大的变化
建议看看 设计模式;