Topic: 有关main()在inheritance中的问题

  Print this page

1.有关main()在inheritance中的问题 Copy to clipboard
Posted by: javaycf
Posted on: 2006-09-01 15:29

在学习Thinking in JAVA中:
有如下疑问:
为什么只执行Cleanser 中的main(),而Depergent中的main()没有被调用?
为什么先调用的是cleanser中的main()?

文件名: Depergent.java
JDK 1.5
TOOL:JCreator.
代码如下:

class Cleanser {
  public String s =new String("Cleanser");
  public void append(String a) { s += a;}
  public void dilute() { append(" dilute ()");}
  public void apply() { append(" apply ()");}
  public void scrub() { append(" scrub ()");}
  public void print() { System.out.print(s);}
  public static void main(String[] args) {
    Cleanser x = new Cleanser();
    x.dilute();
    x.apply();
    x.scrub();
    x.print();
  }
}

public class Depergent extends Cleanser {
  //change a method;
  public void scrub() {
    append(" Depergent.scrub ()");
    super.scrub();
  }
    //call base-class version
    
    //add methods to the interface:
  public void foam() { append(" foam() ");}
  
    //test the new class
  public static void main(String[] args) {
      Depergent x = new Depergent();
      x.dilute();
      x.apply();
      x.scrub();
      x.foam();
      x.print();
      System.out.println(" Testing base class: ");
    //  Cleanser.main(args);
  }
}

2.Re:有关main()在inheritance中的问题 [Re: javaycf] Copy to clipboard
Posted by: zcjl
Posted on: 2006-09-01 16:29

如果使用命令:java Cleanser 将会执行Cleanser类的main方法
如果使用命令:java Depergent 将会执行Depergent类的main方法
所以首先要搞清楚jcreator到底运行的是哪个Class

ps:static方法是不能被override的,所以这两个Class都会有各自不同的main方法,互不干扰

3.Re:有关main()在inheritance中的问题 [Re: javaycf] Copy to clipboard
Posted by: javaycf
Posted on: 2006-09-01 21:00

ok .thanks .


   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