普通的Class可以实现多个接口吗?

Jos*_*hua 33 java multiple-inheritance

我知道接口之间可以有多种继承,例如:

public interface C extends A,B {...} //Where A, B and C are Interfaces
Run Code Online (Sandbox Code Playgroud)

但是有可能从多个接口继承常规类,如下所示:

public class A implements C,D {...} //Where A is a Class and C and D are interfaces
Run Code Online (Sandbox Code Playgroud)

Chr*_*ian 71

Java类只能扩展一个父类.extends不允许多重继承().但是,接口不是类,并且类可以实现多个接口.

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

总之,是的,有可能做到:

public class A implements C,D {...}
Run Code Online (Sandbox Code Playgroud)


Mur*_*nik 9

一句话—​​—是的。实际上,JDK 中的许多类都实现了多个接口。例如,ArrayList农具ListRandomAccessCloneable,和Serializable


jav*_*ker 9

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

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


nab*_*ter 7

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

礼仪示例:

在此处输入图片说明

public interface Mammal {
    void move();
    boolean possessIntelligence();
}
Run Code Online (Sandbox Code Playgroud)
public interface Animal extends Mammal {
    void liveInJungle();
}

Run Code Online (Sandbox Code Playgroud)
public interface Human extends Mammal, TwoLeggedMammal, Omnivore, Hunter {
    void liveInCivilization();
}
Run Code Online (Sandbox Code Playgroud)
public interface Carnivore {
    void eatMeat();
}
Run Code Online (Sandbox Code Playgroud)
public interface Herbivore {
    void eatPlant();
}
Run Code Online (Sandbox Code Playgroud)
public interface Omnivore extends Carnivore, Herbivore {
    void eatBothMeatAndPlant();
}
Run Code Online (Sandbox Code Playgroud)
public interface FourLeggedMammal {
    void moveWithFourLegs();
}
Run Code Online (Sandbox Code Playgroud)
public interface TwoLeggedMammal {
    void moveWithTwoLegs();
}
Run Code Online (Sandbox Code Playgroud)
public interface Hunter {
    void huntForFood();
}
Run Code Online (Sandbox Code Playgroud)
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;
    }
}

Run Code Online (Sandbox Code Playgroud)
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");
    }
}
Run Code Online (Sandbox Code Playgroud)
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");
    }
}
Run Code Online (Sandbox Code Playgroud)