写出以下程序的运行结果。
class  First{
public  First(){
aMethod(); }
public  void  aMethod(){
System.out.println(“in  First  class”);}
}
public  class  Second  extends  First{
public  void  aMethod(){
System.out.println(“in  Second  class”);}
public static void main(String[ ]  args){
new  Second( ); }
}请问为什么结果是
in Second class
呢?

解决方案 »

  1.   


    class First {
    public First() {
    aMethod();//接下面调用aMethod();被子类覆盖,所以打印in Second class;
    } public void aMethod(){
    System.out.println("in First class");}
    }public class Second extends First {
    public Second(){//默认有个空构造方法
    super();//默认有个super();调用父类空构造方法。
    }
    public void aMethod(){
    System.out.println("in Second class");} public static void main(String[] args) {
    new Second();
    }
    }
      

  2.   

    因为你在子类当中重写了啊Method方法
      

  3.   

    new Second( );时会首先调用父类的不带参数的构造方法,但Second中重写了aMethod方法,所以执行的是Second中的aMethod方法
      

  4.   

    public class ChangeStrDemo { 
    public static void changestr(String str){ 
         str="welcome"; 
         } 
         public static void main(String[] args) { 
         String str="1234"; 
         changestr(str); 
         System.out.println(str); 
       }
    }
    上面的问题我懂了,谢谢大侠们的帮助,但是这个问题同样困惑着我,为什么输出结果是1234,而不是welcome呢?
      

  5.   

    这么理解。外面的str跟里面的str不是同一个对象,只是指向同一地址的引用。里面的引用的改变了,但是外面的没有改变。
      

  6.   

    方法中的str在方法结束后参数销毁了,所以在system.out.print(str)中的引用地址是“1234”的地址。