一、定义
Java接口(Interface),是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。
接口定义的一般形式为:
[访问控制符]interface <接口名> {
类型标识符final 符号常量名n = 常数;
返回值类型 方法名([参数列表]);
…
}
二、接口的特点
1、Java接口中的成员变量默认都是public,static,final类型的(都可省略),必须被显示初始化,即接口中的成员变量为常量(大写,单词之间用"_"分隔)
2、Java接口中的方法默认都是public,abstract类型的(都可省略),没有方法体,不能被实例化
3、Java接口中只能包含public,static,final类型的成员变量和public,abstract类型的成员方法
4、接口中没有构造方法,不能被实例化
5、一个接口不能实现(implements)另一个接口,但它可以继承多个其它的接口
6、Java接口必须通过类来实现它的抽象方法
7、当类实现了某个Java接口时,它必须实现接口中的所有抽象方法,否则这个类必须声明为抽象类
8、不允许创建接口的实例(实例化),但允许定义接口类型的引用变量,该引用变量引用实现了这个接口的类的实例
9、一个类只能继承一个直接的父类,但可以实现多个接口,间接的实现了多继承.
三、接口的用法
1、精简程序结构,免除重复定义
比如,有两个及上的的类拥有相同的方法,但是实现功能不一样,就可以定义一个接口,将这个方法提炼出来,在需要使用该方法的类中去实现,就免除了多个类定义系统方法的麻烦。
举例:鸟类和昆虫类都具有飞行的功能,这个功能是相同的,但是其它功能是不同的,在程序实现的过程中,就可以定义一个接口,专门描述飞行。
下图是分别定义鸟类和昆虫类,其都有飞行的方法。
下图定义了接口,其类图如下:
实现代码如下:
1. interface Flyanimal{
2. void fly();
3. }
4. class Insect {
5. int legnum=6;
6. }
7. class Bird {
8. int legnum=2;
9. void egg(){};
10. }
11. class Ant extendsInsect implements Flyanimal {
12. public void fly(){
13. "Ant can fly");
14. }
15. }
16. classPigeon extends Bird implements Flyanimal {
17. public void fly(){
18. "pigeon can fly");
19. }
20. public void egg(){
21. "pigeon can lay eggs ");
22. }
23. }
24. public classInterfaceDemo{
25. public static void main(String args[]){
26. new Ant();
27. a.fly();
28. "Ant's legs are"+ a.legnum);
29. new Pigeon();
30. p.fly();
31. p.egg();
32. }
33. }
程序运行结果:
Ant can fly
Ant'slegs are 6
pigeon can fly
pigeon can lay eggs
二、拓展程序功能,应对需求变化。
假设一个学校接待方面的程序,招待不同身份的人的食宿问题,其对应规则如下:
身份 | 食 | 宿 |
学生 | 食堂 | 宿舍 |
教师 | 教师食堂 | 学校公寓 |
学生家长 | 招待所 | 招待所 |
理论上,当然可以对每个不同身份的人各定义一个对应的类,并实现各自的方法,但是观察这写类,可以归纳出其有一个共同的模板,即“人”的“食、宿”问题。这时候,就可以发挥接口的功能了。实现代码如下:
1. interfacePerson{
2. void eat();
3. void sleep();
4. }
5.
6. class Studentimplements Person{
7. public void eat(){
8. "学生去食堂吃饭!");
9. }
10. public void sleep(){
11. "学生回寝室睡觉!");
12. }
13. }
14.
15. class Teacherimplements Person{
16. public void eat(){
17. "教师去教工餐厅吃饭!");
18. }
19. public void sleep(){
20. "教师回学校公寓睡觉!");
21. }
22. }
23. class Parents implements Person{
24. publicvoid eat(){
25. "家长去招待所饭馆吃饭!");
26. }
27. public void sleep(){
28. "家长回招待所睡觉!");
29. }
30. }
31.
32. public class PersonInterface{
33. public static void main(String[] args)
34. {
35. new Student();
36. p.eat();
37. p.sleep();
38. new Teacher();
39. p.eat();
40. p.sleep();
41. new Parents();
42. p.eat();
43. p.sleep();
44. }
45. }
程序执行结果:
学生去食堂吃饭!
学生回寝室睡觉!
教师去教工餐厅吃饭!
教师回学校公寓睡觉!
家长去招待所饭馆吃饭!
家长回招待所睡觉!
现在需要添加一些功能,即现在需要添加“外宾、上级领导”两类角色,并且以后工具需要还要添加相应的身份角色的人进来,此时,只需要根据需要添加“外宾”类、“领导”类,而主类仍然可以拿来就用,无需进行更多的修改。此时就可以显示出接口的作用了。
在上面的程序中添加如下两个类即可。
1. class Foreign implements Person{
2. publicvoid eat(){
3. "外宾去酒店吃饭!");
4. }
5. public void sleep(){
6. "外宾回酒店睡觉!");
7. }
8. }
9.
10. class Leader implements Person{
11. publicvoid eat(){
12. "领导去宾馆吃饭!");
13. }
14. public void sleep(){
15. "外宾回宾馆睡觉!");
16. }
17. }
而主函数中用法仍然一样。
下面给出完整的代码:
1. interfacePerson{
2. void eat();
3. void sleep();
4. }
5.
6. class Studentimplements Person{
7. public void eat(){
8. "学生去食堂吃饭!");
9. }
10. public void sleep(){
11. "学生回寝室睡觉!");
12. }
13. }
14.
15. class Teacherimplements Person{
16. public void eat(){
17. "教师去教工餐厅吃饭!");
18. }
19. public void sleep(){
20. "教师回学校公寓睡觉!");
21. }
22. }
23. class Parents implements Person{
24. publicvoid eat(){
25. "家长去招待所饭馆吃饭!");
26. }
27. public void sleep(){
28. "家长回招待所睡觉!");
29. }
30. }
31. class Foreign implements Person{
32. publicvoid eat(){
33. "外宾去酒店吃饭!");
34. }
35. public void sleep(){
36. "外宾回酒店睡觉!");
37. }
38. }
39.
40. class Leader implements Person{
41. publicvoid eat(){
42. "领导去宾馆吃饭!");
43. }
44. public void sleep(){
45. "领导回宾馆睡觉!");
46. }
47. }
48.
49. public class PersonInterface{
50. public static void main(String[] args)
51. {
52. new Student();
53. p.eat();
54. p.sleep();
55. new Teacher();
56. p.eat();
57. p.sleep();
58. new Parents();
59. p.eat();
60. p.sleep();
61. new Foreign();
62. p.eat();
63. p.sleep();
64. new Leader();
65. p.eat();
66. p.sleep();
67. }
68. }
程序执行结果:
学生去食堂吃饭!
学生回寝室睡觉!
教师去教工餐厅吃饭!
教师回学校公寓睡觉!
家长去招待所饭馆吃饭!
家长回招待所睡觉!
外宾去酒店吃饭!
外宾回酒店睡觉!
领导去宾馆吃饭!
领导回宾馆睡觉!