我知道接口之间可以进行多重继承,例如:
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
我知道接口之间可以进行多重继承,例如:
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 {...}
总的来说 - 是的。
实际上,JDK 中的许多类都实现了多个接口。例如,ArrayList
实现了 List
、RandomAccess
、Cloneable
和 Serializable
接口。
这是在Java中实现多继承的方式:
public class A implements C,D {...} 是合法的。
需要注意的是,保留原有的 HTML 标签格式。
是的,一个类可以实现多个接口。每个接口提供了某种行为的契约。我附上了详细的类图和接口及类的框架。
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");
}
}
虽然一个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
接口可以扩展其他接口,但是接口不能实现任何其他接口。当涉及到类时,它可以扩展另一个类并实现任意数量的接口。
class A extends B implements C,D{...}
是的,这是可能的。但需要注意的是:Java不支持多重继承,即一个类不能继承多个类。但是,一个类可以实现多个接口。