接口
意义:
- (1)是一种数据类型(引用数据)
(2)由interface定义
(3)只能包含常量和抽象方法
(4)不能被实例化
(5)接口是需要被实现/继承的,实现类/派生类:必须重写接口中的所有抽象方法
(6)一个类可以实现多个接口,用逗号分隔
若又继承又实现时,应先继承后实现
(7)接口可以继承接口
作用:
- 将部分派生类中所共有的行为,抽到接口中(用implements实现接口,可实现多个接口)
- 符合既是也是原则时,使用接口
- 接口是对继承的单根性的扩展--------------多继承
public static void main(String[] args) {
Inter6 o1 = new Doo();
Inter5 o2 = new Doo();
}
}
interface Inter5{
void show();
}
interface Inter6 extends Inter5{
void test();
}
class Doo implements Inter6{
public void show(){}
public void test(){}
}
interface Inter3{
void show();
}
interface Inter4{
void test();
}
abstract class Boo{
abstract void say();
}
class Coo extends Boo implements Inter3,Inter4{
public void show(){}
public void test(){}
public void say(){}
}
interface Inter2{
void show();
void test();
}
class Aoo implements Inter2{
public void show(){};
public void test(){};
}
interface Inter1{
public static final int NUM=5;
public abstract void show();
int COUNT = 5;
void test();
}