当前位置:课程学习>>第四章 面向对象基础>>文本学习>>知识点七


知识点七  接口




一、接口的定义

Java中的接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的声明而没有方法的实现,接口内只有成员和方法声明,因此Java接口比Java抽象类更为抽象。

Java是一种单继承的语言,但是借助于接口,通过让一个类实现多个接口这种方法来实现多重继承。

接口把方法的声明和方法的实现分开,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的功能。

接口的的定义和类的定义类似,包括了接口声明和接口体两个部分,格式为:

[ public ] interface 接口名 [ extends 父接口列表]

{

//常量成员的声明

[public] [static] [final ] 数据类型 成员名=初始值;

……

//抽象方法

[public] [abstract] 返回值类型 方法名([参数列表]) [throws 异常列表];

}

这里需要注意的是:

(1) 声明为public的接口可以被任何其他类所实现,也可以被任何其他接口继承,但必须独立存为一个.java文件,文件名为该接口名。如果没有public修饰,则该接口只能被同一个包内的其他接口继承或其他类实现。

(2) 接口中的所有成员默认均为公共的静态常量成员,其中public static final 修饰符均可以省略。

(3) 接口中的方法都是公共的抽象方法,其中public abstract 也是常常省略掉。

二、接口的实现

在类中实现接口需要在类定义时使用implements关键字来声明,如果有extends关键字,必须放在extends后,形式为:类名 [ extends 父类名 ] implements 接口列表

在类中实现接口时,若该类不是抽象类,则在类的定义部分需要实现而且必须实现(重写)接口中的所有方法。若该类为抽象类,可以不重写接口中的所有方法,不过在该类的任何一个非抽象的直接子类中就必须实现这些方法。

在实现接口中的方法时必须显式地使用public修饰符。

【例4.36】实现接口。

1 //InterfaceTest1.java

2 interface InterfaceExample

3 {

4    int a=4;

5    int b=5;

6    void f1();

7    int f2(int c);

8 }

9 public class InterfaceTest1 implements InterfaceExample

10 {

11    int i;

12    public void f1()

13    {

14       System.out.println("f1="+a);

15    }

16    public int f2(int d)

17    {

18       System.out.println("f2="+d);

19       return d;

20    }

21    public static void main(String[] args)

22    {

23       InterfaceTest1 it=new InterfaceTest1();

24       it.f1();

25       it.f2(b);

26    }

27 };

程序运行结果为:

f1=4

f2=5

这里需要注意的是如果在第2行interface前加上public修饰符,编译时就会出现错误,因为一个.java文件中最多只能有一个public类或接口。

一个类可以同时实现多个接口,从而实现多重继承的功能。

【例4.37】实现多个接口。

1 //InterfaceTest2.java

2 interface Interface1

3 {

4    int a=1;

5    void prt();

6 }

7 interface Interface2

8 {

9    int b=2;

10    int func();

11 }

12 class SuperClass

13 {

14    int c=5;

15    public int func()

16    {

17       System.out.println("func in class");

18       return 3;

19    }

20 };

21 public class InterfaceTest2 extends SuperClass implements Interface1,Interface2

22 {

23    boolean t;

24    public void prt()

25    {

26       System.out.println("prt in InterfaceClass");

27    }

28    public static void main(String[] args)

29    {

30       InterfaceTest2 it=new InterfaceTest2();

31       it.prt();

32       it.func();

33    }

34 };

程序运行结果为:

prt in InterfaceClass

func in class

这里需要注意的是由于在SuperClass已经定义func()方法,而InterfaceTest2通过继承SuperClass类已经具有func()方法,所以在InterfaceTest2类中可以不再重写接口中的func()方法。

三、接口的继承

接口与接口之间可以继承,使用extends关键字,但与类不同的是,一个接口可以同时继承多个接口。

【例4.38】接口的继承。

1 //InterfaceTest3.java

2 interface Interface1

3 {

4    int a=1;

5    void f1();

6 }

7 interface Interface2

8 {

9    int b=2;

10    void f2();

11 }

12 interface Interface3 extends Interface1,Interface2

13 {

14    int c=3;

15    void f3();

16 }

17 public class InterfaceTest3 implements Interface3

18 {

19    public void f1()

20    {

21       System.out.println("f1 in class");

22    }

23    public void f2()

24    {

25       System.out.println("f2 in class");

26    }

27    public void f3()

28    {

29       System.out.println("f3 in class");

30    }

31    public static void main(String[] args)

32    {

33       InterfaceTest3 it=new InterfaceTest3();

34       it.f1();

35       it.f2();

36       it.f3();

37    }

38 };

程序运行结果为:

f1 in class

f2 in class

f3 in class

四、接口的嵌入

接口的定义可以嵌入到另一个类或接口中,但在实现接口时需要注意它们的层次关系,而且更要注意接口中的方法在什么时候什么地方需要被重写。

【例4.39】接口的嵌入。

1 //InnerInterface.java

2 class A

3 {

4    interface B

5    {

6       void f1();

7    }

8 };

9 interface C

10 {

11    void f2();

12    interface D

13    {

14       void f3();

15    }

16 }

17 public class InnerInterface implements A.B

18 {

19    public void f1(){}

20    public static void main(String[] args)

21    {

22       System.out.println("Test over");

23    }

24 };

25 class ClassTest1 implements C

26 {

27    public void f2(){}

28 };

29 class ClassTest2 implements C.D

30 {

31    public void f3(){}

32 };

程序运行结果为:Test over

 

进入知识归纳学习