多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# java 中的接口 > 原文: [https://beginnersbook.com/2013/05/java-interface/](https://beginnersbook.com/2013/05/java-interface/) 在上一个教程中,我们讨论了[抽象类](https://beginnersbook.com/2013/05/java-abstract-class-method/),它用于实现部分抽象。与抽象类不同,接口用于完全抽象。抽象是一个过程,您只显示“相关”数据并“隐藏”用户不必要的对象细节(参见:[抽象](https://beginnersbook.com/2013/03/oops-in-java-encapsulation-inheritance-polymorphism-abstraction/))。在本指南中,我们将介绍 **java** 中的接口,我们使用它的原因以及在 [Java 编程](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/)中使用接口时必须遵循的规则。 ## Java 中的接口是什么? 接口看起来像一个类,但它不是一个类。接口可以像类一样拥有方法和变量,但接口中声明的方法默认是抽象的(只有方法签名,没有正文,请参阅: [Java 抽象方法](https://beginnersbook.com/2014/01/abstract-method-with-examples-in-java/))。此外,在接口中声明的变量是`public static`最后默认。我们将在本指南后面详细介绍。 ## Java 中的接口有什么用? 如上所述,它们用于完全抽象。由于接口中的方法没有正文,因此必须先由类实现它们才能访问它们。实现接口的类必须实现该接口的所有方法。此外,java 编程语言不允许扩展多个类,但是您可以在类中实现多个接口。 **语法:** 通过指定关键字`interface`声明接口。例如: ```java interface MyInterface { /* All the methods are public abstract by default * As you see they have no body */ public void method1(); public void method2(); } ``` ## Java 中的接口示例 这是类实现接口的方式。它必须提供在接口中声明的所有方法的主体,或者换句话说,您可以说该类必须实现接口的所有方法。 > **你知道吗?** 类`implements`接口但是接口`extends`的另一个接口。 ```java interface MyInterface { /* compiler will treat them as: * public abstract void method1(); * public abstract void method2(); */ public void method1(); public void method2(); } class Demo implements MyInterface { /* This class must have to implement both the abstract methods * else you will get compilation error */ public void method1() { System.out.println("implementation of method1"); } public void method2() { System.out.println("implementation of method2"); } public static void main(String arg[]) { MyInterface obj = new Demo(); obj.method1(); } } ``` 输出: ```java implementation of method1 ``` **您可能还想阅读:** [抽象类和接口之间的区别](https://beginnersbook.com/2013/05/abstract-class-vs-interface-in-java/) ## 接口和继承 如上所述,接口不能实现另一个接口。它必须扩展其他接口。请参阅下面的示例,其中我们有两个接口`Inf1`和`Inf2`。`Inf2`扩展了`Inf1`所以如果类实现了`Inf2`,它必须提供`Inf2`和`Inf1`接口的所有方法的实现。 > 在此处了解有关继承的更多信息: [Java 继承](https://beginnersbook.com/2013/03/inheritance-in-java/) ```java interface Inf1{ public void method1(); } interface Inf2 extends Inf1 { public void method2(); } public class Demo implements Inf2{ /* Even though this class is only implementing the * interface Inf2, it has to implement all the methods * of Inf1 as well because the interface Inf2 extends Inf1 */ public void method1(){ System.out.println("method1"); } public void method2(){ System.out.println("method2"); } public static void main(String args[]){ Inf2 obj = new Demo(); obj.method2(); } } ``` 在这个程序中,类`Demo`只实现接口`Inf2`,但是它必须提供接口`Inf1`的所有方法的实现,因为接口`Inf2`扩展了`Inf1`。 ## Java 中的标记或标记接口 空接口称为标记或标记接口。例如,`Serializable`,`EventListener`,`Remote`(`java.rmi.Remote`)是标记接口。这些接口中没有任何字段和方法。[在这里阅读更多相关信息](https://beginnersbook.com/2016/03/tag-or-marker-interfaces-in-java/)。 ### 嵌套接口 在另一个接口或类中声明的接口称为[嵌套](https://beginnersbook.com/2016/03/nested-or-inner-interfaces-in-java/)接口。它们也被称为内部接口。例如,集合框架中的`Entry`接口在`Map`接口内声明,这就是我们不直接使用它的原因,而是我们这样使用它:`Map.Entry`。 **要点:**以下是关于接口的关键要点: 1)我们无法在 java 中实例化接口。这意味着我们无法创建接口的对象 2)接口提供完全抽象,因为它的方法都没有。另一方面,抽象类提供了部分抽象,因为它可以具有抽象和具体(带有正文的方法)方法。 3)类使用`implements`关键字来实现接口。 4)虽然在接口的任何方法的类中提供实现,但它需要被公开提及。 5)实现任何接口的类必须实现该接口的所有方法,否则应该将该类声明为`abstract`。 6)接口不能声明为私有,受保护或瞬态。 7)所有接口方法默认为`abstract`和`public`。 8)接口中声明的变量默认为`public`,`static`和`final`。 ```java interface Try { int a=10; public int a=10; public static final int a=10; final int a=10; static int a=0; } ``` 以上所有陈述都是相同的。 9)接口变量必须在声明时初始化,否则编译器将抛出错误。 ```java interface Try { int x;//Compile-time error } ``` 上面的代码将抛出编译时错误,因为变量`x`的值在声明时未初始化。 10)在任何实现类中,您都不能更改在`interface`中声明的变量,因为默认情况下它们是`public`,`static`和`final`。这里我们实现了具有变量`x`的接口`Try`。当我们尝试设置变量`x`的值时,我们得到了编译错误,因为变量`x`默认是公共静态`final`,并且最终变量无法重新初始化。 ```java class Sample implements Try { public static void main(String args[]) { x=20; //compile time error } } ``` 11)接口可以扩展任何接口但不能实现它。类实现接口和接口扩展接口。 12)**类**可以实现任何**数量的接口**。 13)如果两个接口中存在**两个或多个相同的方法**并且一个类实现两个接口,则该方法的实现一次就足够了。 ```java interface A { public void aaa(); } interface B { public void aaa(); } class Central implements A,B { public void aaa() { //Any Code here } public static void main(String args[]) { //Statements } } ``` 14)类不能实现两个具有相同名称但返回类型不同的方法的接口。 ```java interface A { public void aaa(); } interface B { public int aaa(); } class Central implements A,B { public void aaa() // error { } public int aaa() // error { } public static void main(String args[]) { } } ``` 15)变量名称冲突可以通过接口名称解决。 ```java interface A { int x=10; } interface B { int x=100; } class Hello implements A,B { public static void Main(String args[]) { /* reference to x is ambiguous both variables are x * so we are using interface name to resolve the * variable */ System.out.println(x); System.out.println(A.x); System.out.println(B.x); } } ``` ## java 中接口的优点: 使用接口的优点如下: 1. 在不打扰实现部分的情况下,我们可以实现实现的安全性 2. 在 java 中,不允许[**多重继承**](https://beginnersbook.com/2013/05/java-multiple-inheritance/) ,但是您可以使用接口来使用它,因为您可以实现多个接口。