Java基础:接口interface

示例代码:

接口:使用interface关键字修饰,类似与抽象类,接口里的方法没有方法体,接口提供一组标准,

  1. public interface ICharQueue {          //定义接口接口
  2.     void put(char ch);                          //接口中的方法
  3.     char get();
  4. }
  5.  
  6. class FixedQueue implements ICharQueue{    //使用implement关键字继承接口
  7.     private char[] q;
  8.     private int putid,getid;
  9.     public FixedQueue(int size) {  //构造方法
  10.         q = new char[size];
  11.         putid = getid = 0;
  12.     }
  13.  
  14.     public void put(char ch) {        //必须实现接口的方法
  15.         if(putid == q.length) {
  16.             System.out.println("The Queue is full:");
  17.         }
  18.         q[putid++] = ch;           
  19.     }
  20.     
  21.     public char get() {             //必须实现接口的方法
  22.         if(getid == putid) {
  23.                 System.out.println("The Queue is empty:");
  24.                 return (char) 0;
  25.         }
  26.         return q[getid++];          
  27.     }        
  28. }
  29. public class IQDemo {
  30.  
  31.     public static void main(String[] args) {
  32.     
  33.         FixedQueue fq = new FixedQueue(10);   //FixedQueue 对象
  34.         
  35.         ICharQueue iq;                       //接口变量
  36.         int i;
  37.         char ch;
  38.         System.out.println("this is  fq:");
  39.         iq = fq;                                     //接口变量引用对象,(接口变量可以引用任何实现接口的对象)
  40.         for(i=0;i < 10;i++) {
  41.             iq.put((char) ('A'+i));           //调用方法
  42.         }
  43.         for(i=0;i <10;i++) {
  44.             ch = iq.get();                      //调用方法 
  45.             System.out.print(ch +" ");
  46.         }
  47.         System.out.println();
  48.          
  49.     }
  50. }
  51. -----------------------------------------------------------------------------------
  52. 接口中的常量
  53. public interface IConst {     //接口
  54.     int MIN = 0;                     //接口中的常量
  55.     int MAX = 10;
  56.     String ERRORMSG = "error is boundary error";
  57.     
  58. }
  59. class ConstDemo implements IConst {  //继承接口
  60.  
  61.     public static void main(String[] args) {
  62.  
  63.         int i;
  64.         int[] nums = new int[MAX];    //使用接口常量
  65.         for(i=MIN;i <= 11;i++) {
  66.             if(i >= MAX) {
  67.                 System.out.println(ERRORMSG);
  68.             }else {
  69.             nums[i] = i;
  70.             System.out.println("nums[i]"+ nums[i]);
  71.             }
  72.         }
  73.         System.out.println();
  74.     }
  75. }
  76. -------------------------------------------------------------------------------
  77. 接口可以被拓展
  78. public interface A {     //接口A
  79.     
  80.     void methd1();
  81.     void methd2();
  82.     default int getMethd(){  //默认接口方法,使用default关键字修饰
  83.         return 1;
  84.     }
  85.    static int getMethd1(){  //接口中使用静态方法
  86.         return 0;
  87.     }
  88. }
  89. public interface B extends A{ //接口B继承接口A,拓展了接口A
  90.     
  91.     void methd3();
  92. }
  93.  

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/87941771