普通的类能实现多个接口吗?

84

我知道接口之间可以进行多重继承,例如:

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

2
那是我在尝试谷歌之后要做的第一件事,但我发现了这个问题:https://dev59.com/LWYr5IYBdhLWcg3wytLe,它允许从类中进行多重继承,而我知道在Java中不应该是可能的。然后我想我也可以得到一个“false”的结果,所以我决定在这里问一下。 - Joshua
1
据我所知,该链接并未展示任何关于类的多重继承。 - Sotirios Delimanolis
3
我原本以为应该用“implements”,但由于出现了“extends”这个词,令我感到困惑。 - Joshua
3
这些踩的原因是因为你没有展示出你做了研究,可能��已经做了研究,但你没有呈现出来。 - Sotirios Delimanolis
8个回答

163

一个Java类只能继承一个父类。多重继承(extends)是不允许的。但是接口不是类,因此一个类可以实现多个接口。

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

总之,是的,这是可能的:

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

1
谢谢您的回答,我想知道如何在这种情况下使用“this”来引用特定的接口。 - Rami Alloush
但是为什么这样做是“可以”的呢?据我所知,在Java中不允许多重继承,因为两个父类可能具有相同签名的方法。因此,虽然两个不同的接口可能具有相同签名的方法,但为什么允许实现超过1个接口呢? - Daniel
1
@Daniel 因为如果您有相同方法的两个具体实现,很难确定使用哪一个。在实现接口的情况下,它们都没有具体实现,因此不会引起混淆。将使用您实现类的方法。 - Dave Lugg

12

总的来说 - 是的。 实际上,JDK 中的许多类都实现了多个接口。例如,ArrayList 实现了 ListRandomAccessCloneableSerializable 接口。


11

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

public class A implements C,D {...} 是合法的。

需要注意的是,保留原有的 HTML 标签格式。


10

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

典型的例子:

在这里输入图片描述

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");
    }
}

4
当然...几乎所有的类都实现了多个接口。在Oracle的Java文档的任何页面上,您都可以找到一个名为“所有已实现接口”的子部分。
这里是Date类的示例

4

虽然一个Java类可以同时实现多个接口,但这里有个需要注意的问题。

如果在一个类中尝试实现两个包含具有相同签名但返回类型不同的方法的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

2

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

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

1
实际上,一个接口可以扩展任意数量的接口:https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html - meriam

2

是的,这是可能的。但需要注意的是:Java不支持多重继承,即一个类不能继承多个类。但是,一个类可以实现多个接口。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接