Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

程式設計java多型問題

請建立pdf、doc及xml三個類別並實作interface printer。

為三個類別加入適當的methods,以實作printer中的methods。

在HomeWorkTest加入適當的methods使得程式能夠順利執行。

參考輸出:

This is pdf printer!

This is doc printer!

This is xml printer!

採用下面的程式片段做修改

interface printer{

public String print();

}

class pdf .......{

}

class doc .......{

}

class xml .......{

}

public class HomeWorkTest {

........

public static void main(String[] args) {

pdf p1=new pdf();

doc d1=new doc();

xml x1=new xml();

print(p1);

print(d1);

print(x1);

}

}

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    interface printer{

    public String print();

    }

    class pdf implements printer{

    public String print(){

    return "This is pdf printer!";

    }

    }

    class doc implements printer{

    public String print()

    return "This is doc printer!";

    }

    }

    class xml implements printer{

    public String print()

    return "This is xml printer!";

    }

    }

    public class HomeWorkTest{

    public static void print(printer p){

    System.out.println(p.print());

    }

    public static void main(String[] args) {

    pdf p1=new pdf();

    doc d1=new doc();

    xml x1=new xml();

    print(p1);

    print(d1);

    print(x1);

    }

    }

    2009-03-04 13:02:26 補充:

    不好意思,覆寫的print在參數列後面少加{

    請自行加上,謝謝

    Source(s): 如有錯誤再發問
Still have questions? Get your answers by asking now.