【问题标题】:Can a normal Class implement multiple interfaces?一个普通的类可以实现多个接口吗?
【发布时间】:2014-02-11 08:53:24
【问题描述】:

我知道接口之间的多重继承是可能的,例如:

public interface C extends A,B {...} //Where A, B and C are Interfaces

但是是否可以像这样从多个接口继承常规类:

public class A implements C,D {...} //Where A is a Class and C and D are interfaces

【问题讨论】:

  • 这是我尝试 google 后要做的第一件事,但我发现了这个问题:stackoverflow.com/questions/13389384/… 它允许从类中进行多重继承来编译,我知道这在 Java 中是不可能的。然后我想我也可以得到一个“错误”的结果,所以我决定在这里问。
  • 据我所知,该链接没有显示来自任何地方的类的多重继承。
  • 这就是我最初的想法,但我很困惑,因为使用的是“扩展”而不是“实现”
  • 反对票是因为缺乏展示性研究。您可能已经完成了研究,但您没有展示出来。

标签: java interface multiple-inheritance


【解决方案1】:

是的,这是可能的。这是一个问题:java 不支持多重继承,即类不能扩展多个类。但是类可以实现多个接口。

【讨论】:

    【解决方案2】:

    Java 类只能扩展一个父类。不允许多重继承 (extends)。然而,接口不是类,一个类可以实现多个接口。

    父接口在implements 关键字之后以逗号分隔的列表声明。

    总之,是的,可以这样做:

    public class A implements C,D {...}
    

    【讨论】:

    • 感谢您的回答,我想知道在这种情况下如何使用“this”来引用某个接口。
    • 但是为什么这样做“可以”。据我了解,java中不允许多重继承,因为两个父类可能有一个具有相同签名的方法。因此,虽然两个不同的接口可能有一个具有相同签名的方法 - 为什么它允许实现多个接口?
    • @Daniel 因为如果您有相同方法的两个具体实现,则没有好的方法来确定使用哪一个。在实现接口的情况下,任何一个都没有具体的实现,所以没有混淆。您的实现类的方法就是正在使用的方法。
    【解决方案3】:

    总之——是的。 实际上,JDK 中的许多类都实现了多个接口。例如,ArrayList 实现 List、RandomAccess、Cloneable 和 Serializable。

    【讨论】:

      【解决方案4】:

      公共类 A 实现 C,D {...} 有效

      这是在java中实现多继承的方式

      【讨论】:

        【解决方案5】:

        当然……几乎所有的类都实现了几个接口。在 Oracle 上的任何 java 文档页面上,您都有一个名为“所有已实现的接口”的小节。

        这里是Date 类的example。

        【讨论】:

          【解决方案6】:

          一个接口可以扩展其他接口。一个接口也不能实现任何其他接口。 当涉及到一个类时,它可以扩展另一个类并实现任意数量的接口。

          class A extends B implements C,D{...}
          

          【讨论】:

          【解决方案7】:

          一个java类确实可以同时实现多个接口,但是这里有个catch。 如果在一个类中,你试图实现两个 java 接口,其中包含具有相同签名但返回类型不同的方法,在这种情况下你会得到编译错误。

          interface One
          {
              int m1();
          }
          interface Two
          {
              float m1();
          }
          public class MyClass implements One, Two{
              int m1() {}
              float m1() {}
              public static void main(String... args) {
          
              }
          }
          

          输出:

          prog.java:14: error: method m1() is already defined in class MyClass
              public float m1() {}
                           ^
          prog.java:11: error: MyClass is not abstract and does not override abstract method m1() in Two
          public class MyClass implements One, Two{
                 ^
          prog.java:13: error: m1() in MyClass cannot implement m1() in Two
              public int m1() {}
                         ^
            return type int is not compatible with float
          3 errors
          

          【讨论】:

            【解决方案8】:

            是的,一个类可以实现多个接口。每个接口都为某种行为提供契约。我附上了详细的类图和shell接口和类。

            礼仪示例:

            public interface Mammal {
                void move();
                boolean possessIntelligence();
            }
            
            public interface Animal extends Mammal {
                void liveInJungle();
            }
            
            
            public interface Human extends Mammal, TwoLeggedMammal, Omnivore, Hunter {
                void liveInCivilization();
            }
            
            public interface Carnivore {
                void eatMeat();
            }
            
            public interface Herbivore {
                void eatPlant();
            }
            
            public interface Omnivore extends Carnivore, Herbivore {
                void eatBothMeatAndPlant();
            }
            
            public interface FourLeggedMammal {
                void moveWithFourLegs();
            }
            
            public interface TwoLeggedMammal {
                void moveWithTwoLegs();
            }
            
            public interface Hunter {
                void huntForFood();
            }
            
            public class Kangaroo implements Animal, Herbivore, TwoLeggedMammal {
                @Override
                public void liveInJungle() {
                    System.out.println("I live in Outback country");
                }
            
                @Override
                public void move() {
                    moveWithTwoLegs();
                }
            
                @Override
                public void moveWithTwoLegs() {
                    System.out.println("I like to jump");
                }
            
                @Override
                public void eat() {
                    eatPlant();
                }
            
                @Override
                public void eatPlant() {
                    System.out.println("I like this grass");
                }
            
                @Override
                public boolean possessIntelligence() {
                    return false;
                }
            }
            
            
            public class Lion implements Animal, FourLeggedMammal, Hunter, Carnivore {
                @Override
                public void liveInJungle() {
                    System.out.println("I am king of the jungle!");
            
                }
            
                @Override
                public void move() {
                    moveWithFourLegs();
                }
            
                @Override
                public void moveWithFourLegs() {
                    System.out.println("I like to run sometimes.");
                }
            
                @Override
                public void eat() {
                    eatMeat();
                }
            
                @Override
                public void eatMeat() {
                    System.out.println("I like deer meat");
                }
            
                @Override
                public boolean possessIntelligence() {
                    return false;
                }
            
                @Override
                public void huntForFood() {
                    System.out.println("My females hunt often");
                }
            }
            
            public class Teacher implements Human {
                @Override
                public void liveInCivilization() {
                    System.out.println("I live in an apartment");
                }
            
                @Override
                public void moveWithTwoLegs() {
                    System.out.println("I wear shoes and walk with two legs one in front of the other");
                }
            
                @Override
                public void move() {
                    moveWithTwoLegs();
                }
            
                @Override
                public boolean possessIntelligence() {
                    return true;
                }
            
                @Override
                public void huntForFood() {
                    System.out.println("My ancestors used to but now I mostly rely on cattle");
                }
            
                @Override
                public void eat() {
                    eatBothMeatAndPlant();
                }
            
                @Override
                public void eatBothMeatAndPlant() {
                    eatPlant();
                    eatMeat();
                }
            
                @Override
                public void eatMeat() {
                    System.out.println("I like this bacon");
                }
            
                @Override
                public void eatPlant() {
                    System.out.println("I like this broccoli");
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-10-13
              • 1970-01-01
              • 2015-04-11
              • 2013-05-24
              • 1970-01-01
              相关资源
              最近更新 更多