Java 学习 第六篇;接口

1: 接口定义
修饰符 interface 接口名
{
常量定义;
抽象方法定义;
}
修饰符 interface 接口名 extends 父接口表
{
常量定义;
抽象方法定义;
}
-> 修饰符可以是public 也可以省略具体情况根据编程实际来决定;‘
-> 接口只能继承接口
2:接口的特征;
接口定义的常量属性由于是接口相关的, 接口属性默认为public static final;
接口中不能有构造器和初始化快所以定义属性时必须指定其初值;
3:接口的使用 接口的实现
一个类可以实现多个接口,形式如下
修饰符 class 类名 extends implements 接口表
{
.....
}
举个栗子:

 1 package six;
 2 interface Product
 3 {
 4 int getProduceTime();
 5 }
 6 interface Output
 7 {
 8 int MAX_CACHE_LINE=50;
 9 void out();
10 void getData(String msg);
11 }
12 public class Printer implements Output,Product {
13 private String[] printData=new String[MAX_CACHE_LINE];
14 private int dataNum=0;
15 public void out()
16 {
17 while(dataNum>0)
18 {
19 System.out.println("正在打印"+printData[0]);
20 System.arraycopy(printData, 1, printData, 0, --dataNum);
21 }
22 }
23 public void getData(String msg)
24 {
25 if(dataNum>=MAX_CACHE_LINE)
26 {
27 System.out.println("输入队列已满");
28 }
29 else
30 {
31 printData[dataNum++]=msg;
32 }
33 }
34 public int getProduceTime()
35 {
36 return 45;
37 }
38 public static void main(String [] args)
39 {
40 Output os=new Printer();
41 os.getData("this is data");
42 os.getData("this is the second data");
43 os.out();
44 os.getData("this is the 3rd data");
45 os.getData("this is the 4th data");
46 os.out();
47 Product p =new Printer();
48 System.out.println(p.getProduceTime());
49 }
50 }
51 output:
52 正在打印this is data
53 正在打印this is the second data
54 正在打印this is the 3rd data
55 正在打印this is the 4th data
56 45
View Code

-------------------------------------------哎呀 今天事情比较多就写这么些吧  华丽丽的分割线未完待续----------------------------------------------------------------------------------------------------------


阿南 On the way.
原文地址:https://www.cnblogs.com/RealMan/p/3678751.html