在 Java 中实现多个接口
本文介绍了一个类如何在 Java 中实现多个接口,并列出了一些示例代码来理解该主题。
在 Java 中,接口类似于类,只是它只能有抽象方法。接口被称为类的蓝图,实现接口的类必须为所有抽象方法提供实现或声明抽象本身。
在 Java 中,类只能扩展一个类,但可以实现多个接口。那么,如果有人问你,一个类可以实现多个接口吗?然后,说是。
让我们从一些代码示例开始来理解这个概念。这是多接口实现的一般结构。
class A implements B, C, D
....Z
实现多个接口的 Java 集合中的现有类是:
除了开发者代码,如果我们留意 JDK 源码,我们会发现 Java 使用了 ArrayList、HashMap 类等多种接口实现。
ArrayList<E>
实现多个接口
public class ArrayList<E>
extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
// Implements multiple interfaces
HashMap<K,V>
实现多个接口
public class HashMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, Cloneable, Serializable
// Implements multiple interfaces
在 Java 中实现多个接口
Java 允许一个类实现多个接口。所以,我们可以随心所欲地实现。在这个例子中,我们创建了 3 个接口,然后使用一个类来实现它们。
在使用接口时,请确保该类实现其所有抽象方法。请参考下面的示例,其中我们实现了所有 3 个接口的所有方法。
package javaexample;
interface A {
void showA();
}
interface B {
void showB();
}
interface C {
void showC();
}
public class SimpleTesting implements A, B, C {
public static void main(String[] args) {
SimpleTesting st = new SimpleTesting();
st.showA();
st.showB();
st.showC();
}
@Override
public void showA() {
System.out.println("Interface A");
}
@Override
public void showB() {
System.out.println("Interface B");
}
@Override
public void showC() {
System.out.println("Interface C");
}
}
输出:
Interface A
Interface B
Interface C
接口扩展了 Java 中的多接口
一个接口还可以实现(扩展)多个接口。Java 允许像类一样接口并且可以实现多个接口。
在接口的情况下,我们应该使用 externds
关键字代替 implements
来实现接口。请参考下面的示例。
package javaexample;
interface A {
void showA();
}
interface B {
void showB();
}
interface C {
void showC();
}
interface D extends A, B, C {
void showD();
}
public class SimpleTesting implements D {
public static void main(String[] args) {
SimpleTesting st = new SimpleTesting();
st.showA();
st.showB();
st.showC();
st.showD();
}
@Override
public void showA() {
System.out.println("Interface A");
}
@Override
public void showB() {
System.out.println("Interface B");
}
@Override
public void showC() {
System.out.println("Interface C");
}
@Override
public void showD() {
System.out.println("Interface D");
}
}
输出:
Interface A
Interface B
Interface C
Interface D
重要的是,如果在类中实现接口,则必须提供抽象方法的实现,否则 java 编译器将引发错误。请参考下面的示例。
package javaexample;
interface A {
void showA();
}
interface B {
void showB();
}
public class SimpleTesting implements A, B {
public static void main(String[] args) {
SimpleTesting st = new SimpleTesting();
st.showA();
st.showB();
}
@Override
public void showA() {
System.out.println("Interface A");
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The type SimpleTesting must implement the inherited abstract method B.showB()
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。