Maison  >  Questions et réponses  >  le corps du texte

java - 使用匿名类实例化了一个抽象类之后,如何重写其中的抽象方法并调用?

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();//想在这里重写getDuration方法,如何做到?
    rhythm.perform();
    note.getNoteValue();
    noteCallback.notePlayed(note);
}

--------------------------------------------------------------------

//重写成如下形式
  @Override public long getDuration(){
      return (expectedMs);
   }

代码附上。在play() 方法中如何直接重写Rhythm类中的抽象方法getDuration()呢? 创建rhythm对象是可以用匿名类的方法实例化的,那是实例化之后 想调用一个重写的getDuration()方法,有什么办法么?在不引入子类继承的前提下。

伊谢尔伦伊谢尔伦2744 Il y a quelques jours563

répondre à tous(2)je répondrai

  • 怪我咯

    怪我咯2017-04-18 10:49:54

    Java ne peut pas faire cela, mais il peut utiliser des classes internes pour répondre à cette exigence déguisée

    public abstract class Rhythm {
    
        /**
         * @return duration between {@linkplain Note} in milliseconds
         */
        public abstract long getDuration();
        
        public abstract class InnderClass {
            public abstract long getDuration();
        }
    
        private InnderClass innderClass;
    
        public Rhythm() {
            innderClass = new InnderClass() {
                @Override
                public long getDuration() {
                    return Rhythm.this.getDuration();
                }
            };
        }
    
        public void setInnderClass(InnderClass innderClass) {
            this.innderClass = innderClass;
        }
    
        public void perform() {
            long duration = innderClass.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();//想在这里重写getDuration方法,如何做到?
            rhythm.setInnderClass(rhythm.new InnderClass() {
                
                @Override
                public long getDuration() {
                    // TODO 把实现放到这里
                    return 0;
                }
            });
            rhythm.perform();
            note.getNoteValue();
            noteCallback.notePlayed(note);
        }

    répondre
    0
  • 怪我咯

    怪我咯2017-04-18 10:49:54

    Vous ne pouvez pas le faire, n'est-ce pas ? Si vous savez utiliser Java, ne serait-ce pas un langage dynamique ?

    répondre
    0
  • Annulerrépondre