freeCodeCamp/guide/chinese/java/interfaces/index.md

9.3 KiB
Raw Blame History

title localeTitle
Interfaces 接口

接口

Java中的接口有点像Class但有一个显着的区别 interface _只能_有方法签名字段和默认方法。从Java 8开始您还可以创建默认方法 。在下一个块中,您可以看到一个接口示例:

public interface Vehicle { 
    public String licensePlate = ""; 
    public float maxVel 
    public void start(); 
    public void stop(); 
    default void blowHorn(){ 
      System.out.println("Blowing horn"); 
   } 
 } 

上面的接口包含两个字段两个方法和一个默认方法。单独使用它并没有多大用处但它们通常与Classes一起使用。怎么样很简单你必须确保一些类implements它。

public class Car implements Vehicle { 
    public void start() { 
        System.out.println("starting engine..."); 
    } 
    public void stop() { 
        System.out.println("stopping engine..."); 
    } 
 } 

:rocket: 运行代码

现在,有一个基本规则 Class必须实现Interface中的所有方法。这些方法必须具有_完全相同的_签名名称参数和例外如界面中所述。类_不需要_声明虽然领域只有方法。

接口实例

创建implements任何接口的Java类后可以将对象实例作为接口的实例引用。这个概念类似于继承实例化。

// following our previous example 
 
 Vehicle tesla = new Car(); 
 
 tesla.start(); // starting engine ... 

接口不能包含构造函数方法,因此,您无法创建接口本身的实例。您必须创建一个实现接口的类的实例来引用它。将接口视为空白合同表单或模板。

你能用这个功能做什么?多态性!您只能使用接口来引用对象实例!

class Truck implements Vehicle { 
    public void start() { 
        System.out.println("starting truck engine..."); 
    } 
    public void stop() { 
        System.out.println("stopping truck engine..."); 
    } 
 } 
 
 class Starter { 
    // static method, can be called without instantiating the class 
    public static void startEngine(Vehicle vehicle) { 
        vehicle.start(); 
    } 
 } 
 
 Vehicle tesla = new Car(); 
 Vehicle tata = new Truck(); 
 
 Starter.startEngine(tesla); // starting engine ... 
 Starter.startEngine(tata); // starting truck engine ... 

:rocket: 运行代码

但是多个接口怎么样?

是的,您可以在一个类中实现多个接口。在继承的类中您被限制为仅继承一个类在这里您可以扩展任意数量的接口。但是不要忘记实现_所有_接口的所有方法否则编译将失败

public interface GPS { 
    public void getCoordinates(); 
 } 
 
 public interface Radio { 
    public void startRadio(); 
    public void stopRadio(); 
 } 
 
 public class Smartphone implements GPS,Radio { 
    public void getCoordinates() { 
        // return some coordinates 
    } 
    public void startRadio() { 
      // start Radio 
    } 
    public void stopRadio() { 
        // stop Radio 
    } 
 } 

:rocket: 运行代码

接口的一些功能

  • 您可以在接口中放置变量,但这不是一个合理的决定,因为类不必具有相同的变量。简而言之,避免放置变量!
  • 即使您省略了public关键字,接口中的所有变量和方法都是公共的。
  • 接口无法指定特定方法的实现。它由类来完成。虽然最近有例外(见下文)。
  • 如果一个类实现多个接口那么方法签名重叠的可能性很小。由于Java不允许多个方法具有完全相同的签名因此可能会导致问题。有关详细信息请参阅此问题

接口默认方法

在Java 8之前我们无法指示接口具有特定的方法实现。如果突然改变接口定义这会导致很多混乱和代码中断。

假设您编写了一个包含接口的开源库。比如说您的客户即几乎所有世界各地的开发人员都在大量使用它并且很开心。现在您必须通过向接口添加新方法定义来升级库以支持新功能。但是这会破坏_所有_构建因为实现该接口的所有类现在都必须改变。真是个大灾难

值得庆幸的是Java 8现在为我们提供了接口的default方法。 default方法_可以_ _直接_在Interface中包含自己的实现因此如果类没有实现默认方法编译器将采用接口中提到的实现。不错不是吗因此在您的库中您可以在界面中添加任意数量的默认方法而不必担心破坏任何东西

public interface GPS { 
    public void getCoordinates(); 
    default public void getRoughCoordinates() { 
        // implementation to return coordinates from rough sources 
        // such as wifi & mobile 
        System.out.println("Fetching rough coordinates..."); 
    } 
 } 
 
 public interface Radio { 
    public void startRadio(); 
    public void stopRadio(); 
 } 
 
 public class Smartphone implements GPS,Radio { 
    public void getCoordinates() { 
        // return some coordinates 
    } 
    public void startRadio() { 
      // start Radio 
    } 
    public void stopRadio() { 
        // stop Radio 
    } 
 
    // no implementation of getRoughCoordinates() 
 } 
 
 Smartphone motoG = new Smartphone(); 
 motog.getRoughCoordinates(); // Fetching rough coordinates... 

:rocket: 运行代码

但是,如果两个接口具有相同的方法签名会发生什么?

真棒的问题。在这种情况下如果你没有在Class中提供实现那么糟糕的编译器会感到困惑并且简单地失败您还必须在类中提供默认方法实现。还有一种使用super来调用你喜欢的实现的漂亮方式:

public interface Radio { 
    // public void startRadio(); 
    // public void stopRadio(); 
 
    default public void next() { 
        System.out.println("Next from Radio"); 
    } 
 } 
 
 public interface MusicPlayer { 
    // public void start(); 
    // public void pause(); 
    // public void stop(); 
 
    default public void next() { 
        System.out.println("Next from MusicPlayer"); 
    } 
 } 
 
 public class Smartphone implements Radio, MusicPlayer { 
    public void next() { 
        // Suppose you want to call MusicPlayer next 
        MusicPlayer.super.next(); 
    } 
 } 
 
 Smartphone motoG = new Smartphone(); 
 motoG.next(); // Next from MusicPlayer 

:rocket: 运行代码

接口中的静态方法

Java 8的另一个新功能是能够向接口添加静态方法。接口中的静态方法几乎与具体类中的静态方法相同。唯一的区别是static方法不会在实现接口的类中继承。这意味着在调用静态方法而不是实现它的类时会引用接口。

interface MusicPlayer { 
  public static void commercial(String sponsor) { 
    System.out.println("Now for a message brought to you by " + sponsor); 
  } 
 
  public void play(); 
 } 
 
 
 class Smartphone implements MusicPlayer { 
    public void play() { 
        System.out.println("Playing from smartphone"); 
    } 
 } 
 
 class Main { 
  public static void main(String[] args) { 
    Smartphone motoG = new Smartphone(); 
    MusicPlayer.commercial("Motorola"); // Called on interface not on implementing class 
    // motoG.commercial("Motorola"); // This would cause a compilation error 
  } 
 } 

:rocket: 运行代码

继承接口

在Java中接口可以_继承_另一个接口通过使用你猜对了 extends关键字:

public interface Player { 
    public void start(); 
    public void pause(); 
    public void stop(); 
 } 
 
 public interface MusicPlayer extends Player { 
    default public void next() { 
        System.out.println("Next from MusicPlayer"); 
    } 
 } 

这意味着,实施类MusicPlayer接口必须实现的_所有_方法MusicPlayer以及Player

public class SmartPhone implements MusicPlayer { 
    public void start() { 
        System.out.println("start"); 
    } 
    public void stop() { 
        System.out.println("stop"); 
    } 
    public void pause() { 
        System.out.println("pause"); 
    } 
 } 

:rocket: 运行代码

哎呀,我忘记了next()吗?请注意,因为它是一种default方法,所以我根本不需要提供实现。 不适用于JDK <8

所以现在你已经很好地掌握了接口了解抽象类以了解Java如何为您提供另一种定义合同的方法。