<tfoot id='be7Iz'></tfoot><legend id='be7Iz'><style id='be7Iz'><dir id='be7Iz'><q id='be7Iz'></q></dir></style></legend>
  • <small id='be7Iz'></small><noframes id='be7Iz'>

      <bdo id='be7Iz'></bdo><ul id='be7Iz'></ul>

    1. <i id='be7Iz'><tr id='be7Iz'><dt id='be7Iz'><q id='be7Iz'><span id='be7Iz'><b id='be7Iz'><form id='be7Iz'><ins id='be7Iz'></ins><ul id='be7Iz'></ul><sub id='be7Iz'></sub></form><legend id='be7Iz'></legend><bdo id='be7Iz'><pre id='be7Iz'><center id='be7Iz'></center></pre></bdo></b><th id='be7Iz'></th></span></q></dt></tr></i><div id='be7Iz'><tfoot id='be7Iz'></tfoot><dl id='be7Iz'><fieldset id='be7Iz'></fieldset></dl></div>

        在 Java 类中实现的具有相同方法签名的两个接口

        时间:2023-10-15

          <tbody id='1nbie'></tbody>
        <tfoot id='1nbie'></tfoot>
            • <i id='1nbie'><tr id='1nbie'><dt id='1nbie'><q id='1nbie'><span id='1nbie'><b id='1nbie'><form id='1nbie'><ins id='1nbie'></ins><ul id='1nbie'></ul><sub id='1nbie'></sub></form><legend id='1nbie'></legend><bdo id='1nbie'><pre id='1nbie'><center id='1nbie'></center></pre></bdo></b><th id='1nbie'></th></span></q></dt></tr></i><div id='1nbie'><tfoot id='1nbie'></tfoot><dl id='1nbie'><fieldset id='1nbie'></fieldset></dl></div>

                <legend id='1nbie'><style id='1nbie'><dir id='1nbie'><q id='1nbie'></q></dir></style></legend>

                <small id='1nbie'></small><noframes id='1nbie'>

                • <bdo id='1nbie'></bdo><ul id='1nbie'></ul>
                • 本文介绍了在 Java 类中实现的具有相同方法签名的两个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个 Java 接口和一个实现类.

                  I have two Java interfaces and one implementing class.

                  (我已经使用 Eclipse 直接运行程序,并且我没有尝试通过从命令行显式编译来检查任何编译器警告等.)

                  (I have used Eclipse to run the program directly, and I did not try to check any compiler warning et cetera by explicitly compiling from the command line.)

                  为什么它们可以毫无问题地运行?为什么 Java 允许这样做,即使它满足两个接口的约定"但在实现类时会产生歧义?

                  Why do they run without problem? Why does Java allow this, even when it satisfies the "contract" of both interfaces but create ambiguity in implementing class?

                  更新了示例.

                  public interface CassettePlayer {
                      void play();
                  }
                  
                  public interface DVDPlayer {
                      void play();
                  }
                  
                  public class CarPlayer implements CassettePlayer,DVDPlayer{
                  
                      @Override
                      public void play() {
                          System.out.println("This plays DVD, screw you Cassette !");
                      }
                  
                      public static void main(String args[]) {
                          CarPlayer cp = new CarPlayer();
                          cp.play();
                  
                          CassettePlayer firstInterface = new CarPlayer();
                          firstInterface.play();
                  
                          DVDPlayer secondInterface = new CarPlayer();
                          secondInterface.play();
                      }
                  }
                  

                  推荐答案

                  Java 语言规范,第 8.1.5 节:

                  类中的单个方法声明可以实现多个超接口的方法.例如在代码中:

                  It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:

                  interface Fish { int getNumberOfScales(); }
                  interface Piano { int getNumberOfScales(); }
                  class Tuna implements Fish, Piano {
                     // You can tune a piano, but can you tuna fish?
                     int getNumberOfScales() { return 91; }
                  }
                  

                  Tuna 类中的方法 getNumberOfScales 的名称、签名和返回类型与接口 Fish 中声明的方法相匹配,并且也匹配接口Piano中声明的方法;它被认为是实现两者.

                  the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano; it is considered to implement both.

                  正文接着指出,如果方法签名具有不同的返回类型,例如 doubleint,则无法实现这两个接口会产生相同的类和编译时错误.

                  The text then goes on to note that if the method signatures had different return types, such as double and int, there would be no way to implement both interfaces in the same class and a compile time error would be produced.

                  这篇关于在 Java 类中实现的具有相同方法签名的两个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何抑制特定函数的 Java 编译器警告 下一篇:使用十六进制表示法的 byte[] 数组的文字语法..?

                  相关文章

                  <i id='EtUL6'><tr id='EtUL6'><dt id='EtUL6'><q id='EtUL6'><span id='EtUL6'><b id='EtUL6'><form id='EtUL6'><ins id='EtUL6'></ins><ul id='EtUL6'></ul><sub id='EtUL6'></sub></form><legend id='EtUL6'></legend><bdo id='EtUL6'><pre id='EtUL6'><center id='EtUL6'></center></pre></bdo></b><th id='EtUL6'></th></span></q></dt></tr></i><div id='EtUL6'><tfoot id='EtUL6'></tfoot><dl id='EtUL6'><fieldset id='EtUL6'></fieldset></dl></div>
                  <legend id='EtUL6'><style id='EtUL6'><dir id='EtUL6'><q id='EtUL6'></q></dir></style></legend>

                    • <bdo id='EtUL6'></bdo><ul id='EtUL6'></ul>
                    <tfoot id='EtUL6'></tfoot>

                      <small id='EtUL6'></small><noframes id='EtUL6'>