专注Java教育14年 全国咨询/投诉热线:444-1124-454
星辉LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 java定义接口的关键字之interface

java定义接口的关键字之interface

更新时间:2020-06-02 16:51:23 来源:星辉 浏览2383次

java定义接口的关键字interface,interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体的实现,它允许创建者确定方法名.参数列表和返回类型,但没有任何方法体,接口只提供了形式,而未提供任何具体实现

java定义接口的关键字之interface

一个接口表示:"所有实现了该特定接口的类看起来都像这样".接口被用来建立类与类之间的协议(某些面向对象语言用关键字protocol来完成这一功能.)

要想创建一个接口,需要用interface关键字代替class关键字,就像类一样,可以在interface前添加public关键字(但仅限于该接口在与其同名的文件中被定义),如果不加public,则只有包访问权限,这样就只能在同一个包内可用,接口也可以包含域,但是这些域都隐式地是static和final的

要让一个类遵循某个特定接口(或者是一组接口),需要使用implements关键字,它表示"interface只是它的外貌,但是现在我要声明它如何工作的."除此之外,它看起来很像继承

恰当的原则是优先选择类而不是接口,从类开始,如果接口的必要性变的非常明确,那么就进行重构.

//: interfaces/music5/Music5.java
// Interfaces.
package object;
import static net.mindview.util.Print.*;
enum Note{
    MIDDLE_C,MIDDLE_D,MIDDLE_F
}
interface Instrument {
  // Compile-time constant:
  int VALUE = 5; // static & final 可以声明域,但这些域都隐式地是static和final的
  // Cannot have method definitions:
  void play(Note n); // Automatically public //自动的是public
  void adjust();
}

class Wind implements Instrument {
  public void play(Note n) {
    print(this + ".play() " + n);
  }
  public String toString() { return "Wind"; }
  public void adjust() { print(this + ".adjust()"); }
}

class Percussion implements Instrument {
  public void play(Note n) {
    print(this + ".play() " + n);
  }
  public String toString() { return "Percussion"; }
  public void adjust() { print(this + ".adjust()"); }
}

class Stringed implements Instrument {
  public void play(Note n) {
    print(this + ".play() " + n);
  }
  public String toString() { return "Stringed"; }
  public void adjust() { print(this + ".adjust()"); }
}

class Brass extends Wind {
  public String toString() { return "Brass"; }
}    

class Woodwind extends Wind {
  public String toString() { return "Woodwind"; }
}

public class Music5 {
  // Doesn't care about type, so new types
  // added to the system still work right:
  static void tune(Instrument i) {
    // ...
    i.play(Note.MIDDLE_C);
  }
  static void tuneAll(Instrument[] e) {
    for(Instrument i : e)
      tune(i);
  }    
  public static void main(String[] args) {
    // Upcasting during addition to the array:
    Instrument[] orchestra = {
      new Wind(),
      new Percussion(),
      new Stringed(),
      new Brass(),
      new Woodwind()
    };
    tuneAll(orchestra);
  }
} /* Output:
Wind.play() MIDDLE_C
Percussion.play() MIDDLE_C
Stringed.play() MIDDLE_C
Brass.play() MIDDLE_C
Woodwind.play() MIDDLE_C
*///:~

继承和接口可以在同一个类同时使用

//: polymorphism/Sandwich.java
// Order of constructor calls.
package ch08;
interface FastFood{
    void show();
}

class Meal {
  Meal() { System.out.println("Meal()"); }
}

class Bread {
  Bread() { System.out.println("Bread()"); }
}

class Cheese {
  Cheese() { System.out.println("Cheese()"); }
}

class Lettuce {
  Lettuce() { System.out.println("Lettuce()"); }
}


class Lunch extends Meal {
  Lunch() { System.out.println("Lunch()"); }
}


class PortableLunch extends Lunch {
  PortableLunch() { System.out.println("PortableLunch()");}
}

public class Sandwich extends PortableLunch implements FastFood{ //继承和接口可以在同一个类同时使用
  private Bread b = new Bread();
  private Cheese c = new Cheese();
  private Lettuce l = new Lettuce();
  public void show()
  {
      System.out.println("pushing your sandwich order");
  }
  public Sandwich()
  {
      System.out.println("Sandwich()"); 
      show();
  }
  public static void main(String[] args) {
    new Sandwich();
  }
} /* Output:
Meal()
Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()pushing your sandwich order
*///:~

以上就是星辉java培训机构的小编针对“java定义接口的关键字之interface”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>