Java学习第24天
一、今日内容:第六章接口
1.一个简单的接口例子:
接口是一种特殊的抽象类,接口中只提供了接口的原型,并没有接口的方法实现,且子类中必须提供父类的抽象方法的具体实现。接口可以在内部定义抽象的方法,并且在接口内只能包含抽象的方法和常量,不能有变量,初始化块和构造函数。
接口例子:
interface Student2{
int clas=0604;
void learning();
void eat();
void sleep();
}
public class Interface implements Student2{
public void learning() {
System.out.println("学生正在学习!");
}
public void eat() {
System.out.println("学生正在吃饭!");
}
public void sleep() {
System.out.println("学生正在睡觉!");
}
public static void main(String[] args) {
Interface inter=new Interface();
inter.eat();
inter.sleep();
inter.learning();
}
2.接口的声明
使用interface来定义一个接口,接口分为接口的声明和接口体。
| 接口体 | 常量定义 |
| 方法定义 |
定义接口的基本格式如下:
修饰符 interface 接口名 extends 父接口名列表{
[public][static][final] 常量;
[public][abstract] 方法;
}

二、问题
暂无
三、明日继续接口的学习

浙公网安备 33010602011771号