class Cleanser
{
private String s="Cleanser"; public void append(String a){s+=a;} public void dilute(){append("dilute()");} public void apply(){append("apply()");} public void scrub(){append ("scrub()");} public String toString(){return s;}public static void main(String args[]) { Cleanser x=new Cleanser(); x.dilute();x.apply();x.scrub(); System.out.println(x); }
}
class Detergent extends Cleanser
{
public void scrub() { append("Detergent.scrub()"); super.scrub(); } public void foam(){append("foam()");} public static void main(String args[]) { Detergent x=new Detergent(); x.dilute(); x.apply(); x.scrub(); x.foam(); System.out.println(x); System.out.println("Testing base class:"); Cleanser.main(args); }
}
public class SeventhExercise2 extends Detergent
{
public void scrub() {append("This is my scrub()"); super.scrub(); } public void sterilize() { System.out.println("This is my new function:sterilize()"); }public static void main(String args[]) { SeventhExercise2 se2=new SeventhExercise2(); se2.scrub(); se2.sterilize(); Detergent.main(args); }
}
/這是thinking in java第四版上第129頁練習2的題,我對基類的子類又一次進行繼承后,出現(xiàn)了一些問題:/
孫子類的這個方法輸出不正確,這句話沒有加到s中,最后輸出為
我用eclipse逐步調(diào)試發(fā)現(xiàn),從孫子類的scrub函數(shù)出來之前,這個字符串this is my scrub()已經(jīng)加入進去了,但是從這個函數(shù)出來之后,這個s就被清空了,成了空的,百思不得其解啊,望大神解答,我是個java新手,謝謝啦!?。?/p>