标题:怎么不能运行?
取消只看楼主
jxt598598
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2007-6-13
结帖率:100%
 问题点数:0 回复次数:3 
怎么不能运行?
class Member {
  private static int counter = 0;
  private int id = ++counter;
  public Member() {
    System.out.println(
      "Member constructor " + id);
  }
}

class Rodent3 {
  Member m = new Member();
  public Rodent3() {
    System.out.println("Rodent constructor");
  }
  public void hop() {
    System.out.println("Rodent hopping");
  }
  public void scurry() {
    System.out.println("Rodent scurrying");
  }
  public void reproduce() {
    System.out.println("Making more Rodents");
  }
  public String toString() {
    return "Rodent";
  }
}

class Mouse3 extends Rodent3 {
  Member m = new Member();
  public Mouse3() {
    System.out.println("Mouse constructor");
  }
  public void hop() {
    System.out.println("Mouse hopping");
  }
  public void scurry() {
    System.out.println("Mouse scurrying");
  }
  public void reproduce() {
    System.out.println("Making more Mice");
  }
  public String toString() {
    return "Mouse";
  }
}

class Gerbil3 extends Rodent3 {
  Member m = new Member();
  public Gerbil3() {
    System.out.println("Gerbil constructor");
  }
  public void hop() {
    System.out.println("Gerbil hopping");
  }
  public void scurry() {
    System.out.println("Gerbil scurrying");
  }
  public void reproduce() {
    System.out.println("Making more Gerbils");
  }
  public String toString() {
    return "Gerbil";
  }
}

class Hamster3 extends Rodent3 {
  Member m = new Member();
  public Hamster3() {
    System.out.println("Hamster constructor");
  }
  public void hop() {
    System.out.println("Hamster hopping");
  }
  public void scurry() {
    System.out.println("Hamster scurrying");
  }
  public void reproduce() {
    System.out.println("Making more Hamsters");
  }
  public String toString() {
    return "Hamster";
  }
}


public class E10_RodentInitialization {
  public static void main(String args[]) {
    new Hamster3();
  }
}
输出应该是:
Member constructor 1
Member constructor 2
Rodent constructor
Member constructor 7
Member constructor 8
Hamster constructor
搜索更多相关主题的帖子: Member public println System void 
2008-06-14 10:56
jxt598598
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2007-6-13
得分:0 
回复 2# sunkaidong 的帖子
我用javac命令运行不能成功,但是用JCreator编译器能运行结果是
Member constructor 1
Rodent constructor
Member constructor 2
Hamster constructor
不知道为什么会这样!!!!

qq:304742297
2008-06-14 22:40
jxt598598
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2007-6-13
得分:0 
回复 3# saberasurada 的帖子
这是thinking in java书中的一道练习题的答案,他应该想告诉我们怎样初始化吧

qq:304742297
2008-06-14 22:43
jxt598598
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2007-6-13
得分:0 

qq:304742297
2008-06-16 14:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-219116-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.660618 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved