public abstract class Rhythm {
/**
* @return duration between {@linkplain Note} in milliseconds
*/
public abstract long getDuration();
public void perform() {
long duration = getDuration();
try {
Thread.sleep(duration);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}
}
--------------------------------------------------------------------
@Override public void play(Rhythm rhythm, Note note, NoteCallback noteCallback){
rhythm.getDuration();//想在這里重寫(xiě)getDuration方法,如何做到?
rhythm.perform();
note.getNoteValue();
noteCallback.notePlayed(note);
}
--------------------------------------------------------------------
//重寫(xiě)成如下形式
@Override public long getDuration(){
return (expectedMs);
}
代碼附上。在play() 方法中如何直接重寫(xiě)Rhythm類中的抽象方法getDuration()呢? 創(chuàng)建rhythm對(duì)象是可以用匿名類的方法實(shí)例化的,那是實(shí)例化之后 想調(diào)用一個(gè)重寫(xiě)的getDuration()方法,有什么辦法么?在不引入子類繼承的前提下。
添加回答
舉報(bào)
0/150
提交
取消