你好,游客。请登录,

11.3.3 接口的使用

接口机制可以把设计和实现分离,这在java代码里面是怎么体现的呢?
我们用上面的Math做例子:

public static void main(String[] arg) {
Math m = new MathImpl();
System.out.println(m.abs(-9));
}

注意这条语句:Math m = new MathImpl();
m的类型声明是Math,m的实际类型是MathImpl。调用的函数abs的具体是MathImpl里实现的
其实类似的语法我们已经接触过,那就是List的使用:

List list=new ArrayList();

在这里List就是接口,ArrayList就是具体的类型。