刚刚在看《深入浅出java虚拟机》的时候,发现了一个比较不错的题目,分享一下:class Superclass{

public Superclass(){
interestingMehod();
}

void exampleMethod(){
interestingMehod();
}

private void interestingMehod(){ //注释
System.out.println(
"Superclass's interesting method.");
}


}class Subclass extends Superclass{
public Subclass(){
interestingMehod();
}

public void interestingMehod(){
System.out.println(
"Subclass' interesting method.");
}

}
//Test.java
public class Test{
public static void main(String[] args){
Subclass me = new Subclass();
me.exampleMethod();
}
}上面的代码输出什么呢?然后把“注释”处的函数的private改为protected,public或直接去掉,又怎么样呢?

解决方案 »

  1.   

    "Superclass's interesting method."
    不运行程序的话,觉得是这个结果
      

  2.   

    不作更改感觉输出是
    "Superclass's interesting method.
    "Subclass' interesting method.
    "Superclass's interesting method.
      

  3.   

    Superclass's interesting method.
    Subclass' interesting method.
    Superclass's interesting method.一楼忘了 Subclass me = new Subclass();
      

  4.   

    改为protected,public或直接去掉
    就是重写父类方法了
    Subclass' interesting method. 
    Subclass' interesting method. 
    Subclass' interesting method. 
      

  5.   

    呵呵,知道了
    没有看清楚private关键字,还以为方法被重写了呢
      

  6.   


    输出都是
    Superclass's interesting method.
    Subclass' interesting method.
    Superclass's interesting method.
      

  7.   

    在多态中,如果A方法调用B方法,如果B方法是private的,那么调用的B方法是和A方法在同一个类中的B方法。
      

  8.   

    晕,我也没看清那个private 
      

  9.   

    不更改 我觉得是
    "Superclass's interesting method". 
    "Subclass' interesting method."
    "Superclass's interesting method".
    更改后,我觉得是
    "Subclass' interesting method."
    "Subclass' interesting method."
    "Subclass' interesting method."
      

  10.   

         
     zheng jia renqi
      

  11.   

    正确结果是:
    Superclass's interesting method.
    Subclass' interesting method.
    Superclass's interesting method.分析:
    创建一个对象并对其实例化
    Subclass me = new Subclass();
    实例化的过程是:执行构造函数
     public Subclass(){
            interestingMehod();
    }因为构造函数继承Superclass类
    所以调用Superclass中interestingMehod();方法
    然后执行构造函数第二个方法
    public void interestingMehod(){
            System.out.println(
                    "Subclass' interesting method.");
    最后使用对象。方法执行方法
    要是弄懂这个问题最好是去C++的继承 你就明白了
      

  12.   

    Superclass's interesting method.
    Subclass' interesting method.
    Superclass's interesting method.
      

  13.   

    不该的话是:
    Superclass's interesting method.
    Subclass' interesting method.
    Superclass's interesting method.
    改过之后全部是:
    Subclass' interesting method.不改:
    1. 调用之类构造函数前要调用父类的构造函数,所以先调用父类的interestingMehod(),
    然后调用子类的interestingMehod(),然后再调用父类的exampleMethod()。
      

  14.   

    Superclass's interesting method.
    Subclass' interesting method.
    Superclass's interesting method.  说明: 在任何情况下, 如果父类有无参的构造函数, 那么先运行父类的构造函数,再运行子类的构造函数
      

  15.   

    Superclass's interesting method.
    Subclass' interesting method.
    Superclass's interesting method.