迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中实现多个接口

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文介绍了一个类如何在 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()

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便