"类不是抽象的,不会覆盖类中的方法"错误,但我实现了这些方法

时间:2017-04-29 16:40:27

标签: java interface override abstract-class implements

我知道Java中存在许多与此编译器错误相关的问题。 我必须编写一个播放图像,音频文件和视频的简单媒体播放器的行为。我写了七个类,其中三个是接口。这是一个示意图:

  • AudioI包含与音量相关的方法:更弱,更响亮,getVolume和setVolume;
  • VideoI包含与亮度相关的方法:更亮,更暗,getBrightness和setBrightness;
  • MediaI包含媒体的常规方法:play,getTitle,setTitle,getDuration和setDuration。

其他课程是:

  • 音频:它实现了AudioI,MediaI和扩展媒体;
  • 图片:它实现了VideoI,MediaI并扩展了Media;
  • 视频:它实现了VideoI,MediaI和扩展媒体;
  • 媒体:它实现了AudioI,VideoI和MediaI;
  • InterfaceTest:我在哪里测试方法和构造函数。

现在,我遇到的问题是,即使我认为我已正确实现了所有内容,但我在标题中遇到了编译器错误。此外,每次编译时,编译器仅输出4个错误,与音频,图像,视频和媒体类相关。如果我在随机接口中删除一个签名,编译器会切换到另一个方法并告诉我它被错误地实现了。即使我删除了除1之外的所有方法签名,编译器也会输出有关该特定方法的错误。

另外,我不知道这是否有用,我使用Java 8而且我没有使用IDE,我从Windows终端编译并运行文件。

以下是代码:

AudioI界面

public interface AudioI{
  public void louder();
  public void weaker();
  public int getVolume();
  public void setVolume();
}

VideoI界面

public interface VideoI{
  public void brighter();
  public void darker();
  public int getBrightness();
  public void setBrightness();
}

MediaI界面

public interface MediaI{
  public void play();
  public int getDuration();
  public void setDuration();
  public String getTitle();
  public void setTitle();
}

音频课

public class Audio extends Media implements AudioI, MediaI{
  private int volume;
  private int duration;
  private String title;

  /**
    CONSTRUCTORS
  **/
  Audio(){
    this.title = "audio.mp3";
    this.duration = 100;
    this.volume = 5;
  };

  Audio(String title, int duration){
    this.title = title;
    this.duration = duration;
    this.volume = 5;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/
  public void louder(){
    if(this.volume < 10){
        this.volume++;
    }else{
        System.out.print("Maximum volume reached");
    }
  };

  public void weaker(){
    if(this.volume > 0){
        this.volume--;
    }else{
        System.out.print("Minimum volume reached");
    }
  };

  public int getVolume(){
    return this.volume;
  };

  public void setVolume(int volume){
    if(volume > 9){
        System.out.println("******************************");
        System.out.println("* ERROR: maximum volume is 9 *");
        System.out.println("******************************");
    }else if(volume < 0){
        System.out.println("******************************");
        System.out.println("* ERROR: minimum volume is 0 *");
        System.out.println("******************************");
    }else{
        this.volume = volume;
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITLE: " + this.title);

    System.out.print("VOLUME: >");
    for(i = 0; i < this.volume; i++){
        System.out.print(")");
    }
    System.out.println("");
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    this.duration = duration;
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

图像类

public class Image extends Media implements VideoI, MediaI{
  private String title;
  private int brightness;
  private int duration = 1;

  /**
    CONSTRUCTORS
  **/
  Image(){
    this.title = "img.jpeg";
    this.brightness = 5;
  };

  Image(String title){
    this.title = title;
    this.brightness = 5;
    this.duration = 1;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/
  public void brighter(){
    if(this.brightness < 10){
        this.brightness++;
    }else{
        System.out.print("Maximum brightness reached.");
    }
  };

  public void darker(){
    if(this.brightness > 0){
        this.brightness--;
    }else{
        System.out.print("Minimum brightness reached.");
    }
  };

  public int getBrightness(){
    return this.brightness;
  };

  public void setBrightness(int brightness){
    if(brightness > 10){
        System.out.println("* ERROR: maximum brightness is 10 *");
    }else if(brightness < 1){
        System.out.println("* ERROR: minimum brightness is 1 *");
    }else{
        this.brightness = brightness;
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    System.out.print("LUMINOSITA': >");
    for(i = 0; i < this.brightness; i++){
        System.out.print("*");
    }
    System.out.println("");
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot set image duration");
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

视频课

import java.util.ArrayList;

public class Video extends Media implements VideoI, MediaI{
  private int duration;
  private String title;
  private int brightness;
  private Image buff;
  ArrayList<Image> images = new ArrayList<Image>();

  /**
    CONSTRUCTORS
  **/
  Video(){
    for(int i = 0; i < 100; i++){
        images.add(new Image("img" + i + ".jpeg"));
    }
    this.brightness = 5;
    this.duration = 100;
    this.title = "slideshow.mkv";
  };

  Video(String title, int nOfImages){
    for(int i = 0; i < nOfImages; i++){
        buff = new Image("img" + i + ".jpeg");
        this.images.add(buff);
    }
    this.brightness = this.images.get(0).getBrightness();
    this.duration = nOfImages;
    this.title = title;
  };

  /**
    VIDEOI METHODS IMPLEMENTATION
  **/
  public void brighter(){
    int i;

    if(this.brightness < 10){
        this.brightness++;

        for(i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(this.brightness);
        }
    }else{
        System.out.print("Maximum brightness reached.");
    }
  };

  public void darker(){
    int i;

    if(this.brightness > 0){
        this.brightness--;

        for(i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(this.brightness);
        }
    }else{
        System.out.print("Minimum brightness reached.");
    }
  };

  public int getBrightness(){
    return this.brightness;
  };

  public void setBrightness(int brightness){
    if(brightness < 0){
        System.out.println("**********************************");
        System.out.println("* ERROR: minimum brightness is 0 *");
        System.out.println("**********************************");
    }else if(brightness > 10){
        System.out.println("***********************************");
        System.out.println("* ERROR: maximum brightness is 10 *");
        System.out.println("***********************************");
    }else{
        for(int i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(brightness);
            this.brightness = brightness;
        }
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    System.out.print("LUMINOSITA': >");
    for(i = 0; i < this.brightness; i++){
        System.out.print("*");
    }
    System.out.println("");


    System.out.print("DURATA VIDEO: ");
    for(i = 0; i < this.images.size(); i = i + 10){
        System.out.print("[] ");
    }
    System.out.println("");
  };

  public int getDuration(){
    this.duration = this.images.size();
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot set video duration");
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

媒体课

import java.util.ArrayList;

public class Media implements AudioI, VideoI, MediaI{
  private Video video;
  private Audio audio;
  private String title;
  private int duration;
  private int volume;
  private int brightness;
  private boolean hasVideo;
  private boolean hasAudio;

  /**
    CONSTRUCTORS
  **/

  Media(){
    this.title = "Video.mkv";
    this.volume = 5;
    this.brightness = 5;
    this.hasVideo = false;
    this.hasAudio = false;
  };

  Media(String title, Audio audio, Video video){
    this.video = video;
    this.audio = audio;
    this.title = title + ".mkv";
    this.volume = audio.getVolume();
    this.brightness = video.getBrightness();
    if(this.audio.getDuration() >= this.video.getDuration()){
        this.duration = this.audio.getDuration();
    }else{
        this.duration = this.video.getDuration();
    }
    this.hasVideo = true;
    this.hasAudio = true;
  };

  Media(String title, Audio audio){
    this.audio = audio;
    this.title = title + ".mp3";
    this.volume = audio.getVolume();
    this.duration = this.audio.getDuration();
    this.hasVideo = false;
    this.hasAudio = true;
  };

  Media(String title, Video video){
    this.video = video;
    this.title = title + ".sdw";
    this.brightness = video.getBrightness();
    this.duration = this.video.getDuration();
    this.hasVideo = true;
    this.hasAudio = false;
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/

  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    if(this.hasAudio == true){
        System.out.print("VOLUME: >");
        for(i = 0; i < this.audio.getVolume(); i++){
            System.out.print(")");
        }
        System.out.println("");
    }

    if(this.hasVideo == true){
        System.out.print("LUMINOSITA': >");
        for(i = 0; i < this.video.getBrightness(); i++){
            System.out.print("*");
        }
        System.out.println("");
    }

    if(this.hasVideo == true && this.hasAudio == true){
        System.out.print("DURATA AUDIO: ");
        for(i = 0; i < this.video.getDuration(); i = i + 10){
            System.out.print("[] ");
        }
        System.out.println("");
        System.out.print("DURATA VIDEO: ");
        for(i = 0; i < this.audio.getDuration(); i = i + 10){
            System.out.print("[] ");
        }
        System.out.println("");
    }
  };

  public void setAudio(Audio audio){
    this.audio = audio;
  };


  public void setVideo(Video video){
    this.video = video;
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot modify media duration");
  };


  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/

  public void louder(){
    this.audio.louder();
  };

  public void weaker(){
    this.audio.weaker();
  };

  public int getVolume(){
    return this.audio.getVolume();
  };

  public void setVolume(int volume){
    this.audio.setVolume(volume);
  };

  /**
    VIDEOI METHODS IMPLEMENTATION
  **/

  public void brighter(){
    this.video.brighter();
  };

  public void darker(){
    this.video.darker();
  };

  public int getBrightness(){
    return this.video.getBrightness();
  };

  public void setBrightness(int brightness){
    this.video.setBrightness(brightness);
  };
}

2 个答案:

答案 0 :(得分:2)

问题似乎与您的set方法有关,在接口中,您已声明set方法将一个参数,但在 classes 中实现那些接口,你为它们提供了参数,即你 interfaces ,而是定义一个与 interfaces 中的方法完全不同的新方法。

解决方案只是更改接口set方法定义以匹配中的方法定义。反之亦然。

答案 1 :(得分:-1)

由于Media类已经实现了AudioI,VideoI和MediaI接口,因此音频和视频类不会实现接口,而是看到发生的情况