我有4個班。一個是驅動程序類,一個是接口和兩個子類。這是接口類:執行不起作用
public interface Speaker {
public void speak();
public void announce(String announcement);
}
這是驅動器類:
public abstract class SpeakerDriver implements Speaker {
public static void main (String [] args) {
Speaker ref;
Politician poli = new Politician("George Bush");
Lecturer lect = new Lecturer("Professor Cross");
Pastor past = new Pastor ("P.M. Smith");
ref = poli;
ref = lect;
ref = past;
System.out.println();
System.out.println(lect);
System.out.println(past);
}
}
,這些都是兩個子類:
public class Pastor implements Speaker {
protected String announcement;
public Pastor (String name) {
}
@Override
public void speak() {
}
@Override
public void announce(String announc) {
System.out.println("I am the pastor at CCBC.");
}
}
public class Lecturer implements Speaker {
protected String announcement;
public Lecturer(String name) {
}
@Override
public void speak() {
}
@Override
public void announce(String announcement) {
System.out.println("I am the lecturer at CCBC");
}
}
我的問題是,什麼是錯的我實現使得輸出如下所示:
[email protected] [email protected]
當它們應該包含println中的文本時。
看到一個參數被傳遞給'announce'然後丟在地板上有點奇怪。我知道這是一個接口要求,但是這應該強制你以某種方式使用該參數......並且,'announce'不會返回'String'有點怪異。只是在說'。 – Makoto
你不給演講者一個名字,因爲你沒有在構造函數中保存它們的值。這樣你可以覆蓋'toString()'。 – Mordechai