Topic: 一个很简单的问题,求解

  Print this page

1.一个很简单的问题,求解 Copy to clipboard
Posted by: ice0819
Posted on: 2009-10-14 22:21

class Base{
  static int y=2;

  Base(int x){
    this();// 似乎没有被调用
    y*=2;
    System.out.println("constructer in base\t"+y);  
  }

  Base(){
   y++;
   System.out.println("default constructer in base\t"+y);
  }
}

class Sub extends Base{

  Sub(){
    super(y);
    y+=3;
  }

  public static void main(String args[]){

    new Sub();
    System.out.println(y);
    
  }

}
为什么this()没被调用

2.Re:一个很简单的问题,求解 [Re: ice0819] Copy to clipboard
Posted by: ggggwffgqeg
Posted on: 2009-10-23 02:02

怎么没被调用,结果是
default constructer in base 3
constructer in base 6
9
楼主自己再运行一下试试。

3.Re:一个很简单的问题,求解 [Re: ice0819] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2009-10-23 18:35

this() invokes the default constructor with no args. Although it is not a common practice to do that, it is still valid.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923