Java-接口
时间:2020-02-23 14:36:39  来源:igfitidea点击:
在本教程中,我们将学习Java编程语言中的接口。
在上一教程中,我们了解了抽象以及如何创建抽象类。
随时检查一下。
什么是接口?
Java中的接口是具有静态常量和抽象方法的类。
它为我们提供了完整的抽象,即接口将实现部分留给了类。
如何创建接口?
我们使用关键字interface来创建Java接口。
接口语法
以下是接口的语法。
interface SomeName {
  retType methName(param_list);
  type varName = val;
}
其中," SomeName"是接口的名称。
methName是具有返回类型retType的方法的名称。
参数列表由param_list表示。
varName是静态常量,并分配有val值。
要记住的要点
- 我们使用" interface"关键字创建接口。 
- 接口内部的方法都是抽象方法。 
- 接口内的变量都是静态最终常量。 
范例1:
在下面的示例中,我们将创建一个接口" RandNum"。
它有一个getNumber()方法。
它具有两个整数常量" MAX"和" MIN"。
interface RandNum {
  int getNumber();
  int MAX = 10;
  int MIN = 1;
}
转换
在编译后在示例1中创建的接口" RandNum"将转换为以下内容。
interface RandNum {
  public abstract int getNumber();
  public static final int MAX = 10;
  public static final int MIN = 1;
}
接口中的所有方法都是抽象方法。
接口中的所有变量都是静态最终常量。
实现接口
当类实现接口时,我们使用关键字" implements"。
语法
以下是实现接口的类的语法。
class ClName implements interface1 {
  //some code goes here...
}
其中," ClName"是类的名称,它正在实现接口" interface1"。
类可以实现多个接口
一个类也可以实现多个接口。
为此,我们列出了逗号分隔的接口名称。
以下是实现多个接口的类的语法。
class ClName implements interface1, interface2, interface3 {
  //some code goes here...
}
其中," ClName"是类的名称,它实现了三个接口" interface1"," interface2"和" interface3"。
范例2:
在下面的示例中,我们有一个类RandNumGenerator,它实现了RandNum接口。
class RandNumGenerator implements RandNum {
  
  /**
   * This method will return a random integer number.
   *
   * This method is from the interface RandNum.
   * It was abstract and now we are implementing it
   * inside this RandNumGenerator class.
   */
  public int getNumber() {
    /**
     * Using random method of the Math class to
     * get a random floating point number
     * greater than or equal to 0 and
     * less than 1.
     */
    double rand = Math.random();
    /**
     * MAX and MIN are the two static final constants
     * from the interface RandNum.
     *
     * We are using it here in this method to compute
     * an integer random number between MIN and Max
     * both inclusive.
     */
    double randNum = Math.floor((rand * MAX) + MIN);
    /**
     * Returning the random number.
     *
     * Since, the return type of this method is int
     * so, we are type-casting the value to int.
     */
    return (int)randNum;
  }
  /**
   * This will return the value stored in the
   * constant MAX from the RandNum interface.
   */
  public int getMAX() {
    return MAX;
  }
  /**
   * This will return the value stored in the
   * constant MIN from the RandNum interface.
   */
  public int getMIN() {
    return MIN;
  }
}
接口的部分实现
如果一个类没有完全实现接口,则必须将其声明为"抽象"类。
在下面的示例中,我们有一个Sample类,它没有完全实现RandNum接口。
因此,我们将其声明为抽象类。
abstract class Sample implements RandNum {
  
  public void greetings() {
    System.out.println("Hello from the Sample class...");
    /**
     * Note! the getNumber() method from the
     * interface RandNum is not implemented here
     * in this Sample class so, we have declared
     * this class as abstract.
     */
  }
  
}
完整代码:RandNum接口
/**
 * RandNum interface
 */
interface RandNum {
  int getNumber();
  int MAX = 10;
  int MIN = 1;
}
/**
 * RandNumGenerator class implementing
 * the RandNum interface.
 */
class RandNumGenerator implements RandNum {
  
  public int getNumber() {
    double rand = Math.random();
    double randNum = Math.floor((rand * MAX) + MIN);
    return (int)randNum;
  }
  public int getMAX() {
    return MAX;
  }
  public int getMIN() {
    return MIN;
  }
}
/**
 * main class
 */
public class Example {
  public static void main(String[] args) {
    //create object
    RandNumGenerator rngObj = new RandNumGenerator();
    //get a random number
    int n = rngObj.getNumber();
    //get max
    int max = rngObj.getMAX();
    //get min
    int min = rngObj.getMIN();
    //output
    System.out.println("Max: " + max);
    System.out.println("Min: " + min);
    System.out.println("Random number: " + n);
  }
}
Max: 10 Min: 1 Random number: 7

