接口是JAVA种最重要的概念,接口可理解 为一种特殊类,里面全是由全局常量和公共的抽象方法组成 的
接口格式:
interface NAME{
全局常量
抽象方法
}
注意:接口需全部大写
接口的实现也必须通过子类,使用关键词implements,而且接口是可以多实现的
一个接口不能继承一个抽象类,但是可以通过extends同时继承多个接口,实现多接口继承
案例:
interface Inter{
public static final int AGE=100;
public abstract void tell();
}
interface Inter2{
public abstract void say();
}
//接口继承
interface Inter3 extends Inter,Inter2{
}
abstract class Abs1{
public abstract void print();
}
//多接口/同时继承和实现接口
class A extends Abs1 implements Inter,Inter2{
//重写所有抽象方法
public void tell() {
}
public void say() {
}
public void print() {
}
}
public class Test03 {
public static void main(String[] args) {
//Inter i =new Inter();//错误,接口须通过子类实现
A a=new A();
a.tell();
System.out.println(Inter.AGE);
a.say();
}
694

被折叠的 条评论
为什么被折叠?



