javaycf
发贴: 7
积分: 0
|
于 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); } }
|