【问题标题】:One class implements from two different interfaces in Java一个类从 Java 中的两个不同接口实现
【发布时间】:2016-11-06 18:21:43
【问题描述】:

我有两个类从两个接口实现。 这是我的界面:

interface Identifiable {

     int getId();

}

interface Greetable {
    String helloMessage();
    String byeMessage();    
}

这是我的课:

public class Lecturer implements Greetable, Identifiable {

    private int employeeId;
    private String name;
    private String title;

    @Override
    public String helloMessage() {
        return name;
    }

    @Override
    public String byeMessage() {
        return title;
    }   

}

public class Student implements Greetable, Identifiable {

    private char examScore;

    @Override
    public String helloMessage() {
        return "Hi";
    }

    @Override
    public String byeMessage() {
        return "Whats up";
    }   


}

我从必须从接口抽象方法的类中得到错误?这是什么意思?

【问题讨论】:

    标签: java class interface


    【解决方案1】:

    非抽象类需要创建在它们正在实现的任何接口中找到的任何方法的具体版本,而当您的类实现接口之一的具体版本时,Greetable 接口,您并没有实现所有两个接口的方法,这里两个类都缺少 Identifiable 接口中的 public int getId() 方法。

    解决方案:给这两个类一个 int id 字段以及返回该字段所持有值的 getId() 方法。

    例如对于学生,

    public class Student implements Greetable, Identifiable {
    
        private char examScore;
        private int id;  // **** your classes will need this field ****
    
        // need to set the ID somehow, either with a setter or a constructor
        public Student(int id) {
            this.id = id;
        }
    
        @Override
        public String helloMessage() {
            return "Hi";
        }
    
        @Override
        public String byeMessage() {
            return "Whats up";
        }
    
        @Override  // **************** add this method to return the value held by id ******
        public int getId() {
            return this.id;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您定义实现两个接口,但您只实现了第二个接口的方法。 所以你必须在两个类中实现getId()方法。

      【讨论】:

        【解决方案3】:

        您还没有在Identifiable 中实现getId() 方法。如果您没有实现该方法,则需要将LecturerStudent 设为抽象,或者您需要在这两个类中实现getId() 方法。

        在您的情况下,我认为您需要创建 StudentLecturer 的实例。如果是这样,那么您不能将它们设为抽象,因为无法创建抽象类实例。所以最好在两个类中实现getId()

        【讨论】:

          【解决方案4】:

          您的StudentLecturer 类必须同时实现GreetableIdentifiable interface 方法,否则它们需要声明为abstract 类,即,您从Identifiable 中缺少getId()导致问题的interface,更正了下面的代码。

          讲师班:

          public class Lecturer implements Greetable, Identifiable {
              int getId() {
                 return employeeId;
              }
          
             //all other existing methods
          }
          

          学生班:

          public class Student implements Greetable, Identifiable {
                  int getId() {
                     return studentId;
                  }
          
                 //all other existing methods
              }
          

          你可以看here

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-05-10
            • 2016-07-23
            • 1970-01-01
            • 1970-01-01
            • 2023-03-06
            • 2012-08-02
            • 1970-01-01
            相关资源
            最近更新 更多