繁体   English   中英

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

[英]Can a normal Class implement multiple interfaces?

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

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

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

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

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

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

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

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

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

是的,一个类可以实现多个接口。 每个接口都为某种行为提供契约。 我附上了一个详细的类图和 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");
    }
}

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

这是Date类的示例

诚然,一个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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM