接口是多个类的公共规范。
接口是一种引用类型,最重要的内容就是其中的抽象方法。
接口定义
接口是多个类的公共规范。
接口是一种引用类型,最重要的内容就是其中的抽象方法。
接口格式
1
2
3
|
public interface 接口名称{
//接口内容
}
|
如果是Java7,接口内容应该有,
-
常量
-
抽象方法
如果是Java8,接口可以有,
- 默认方法
- 静态方法
如果是Java9,接口可以有
- 私有方法
抽象方法定义
在任何版本的Java中,都能定义接口方法,
格式:
1
|
public abstract 返回值类型 方法名称(参数列表);
|
注意事项:
- 接口中抽象方法,修饰符必须为两个固定关键字,public,abstract
- 两个修饰符可以选择性省略
1
2
3
4
5
|
public abstract void methodsAbs();
abstract void methodsAbs1();
public void methodsAbs2();
void methodsAbs3();
都是对的
|
接口使用步骤
- 接口不能直接使用,必须有一个实现类来实现该接口
格式:
1
2
3
|
public class 实现类名称 implements 接口名称{
...
}
|
- 接口的实现必须覆盖重写(实现)接口中所有抽象方法
实现:去掉abstract关键字,加上方法体大括号。
- 创建实现类的对象,进行使用
1
2
3
4
5
6
7
8
9
10
11
12
|
public interface MyInterfaceAbstract{
public abstract void methodAbs();//抽象方法
}
public class MyInterfaceimpl implements MyInterfaceAbstract{
@override
public void methodAbs(){
System.out.println("吃饭");
}
}
|
1
2
3
4
5
6
7
8
|
public class Demo{
public class void main(String[] args){
MyInterfaceimpl impl = new MyInterfaceimpl();
impl.methodAbs();
}
}
//results: 吃饭
|
默认方法
问题描述:所有实现类都以使用,但接口想添加新方法,所有实现类必须覆盖重写又一次
从java8开始接口允许定义默认方法
格式:
1
2
3
|
public default 返回值类型 方法名称(参数列表){
方法体
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public interface MyInterface{
public abstract void methodAbs();
public default void methodDefult(){
System.out.println("Let's dance");
}
}
public class MyInterfaceimpl implements MyInterface{
@override
public void methodAbs(){
System.out.println("111");
}
}
|
1
2
3
4
5
6
7
8
9
10
|
public class Demo{
public static void main(String[] args){
MyInterfaceimpl impl = new MyInterfaceimpl();
impl.methodAbs();
impl.methodDefault();
}
}
//results: 111
// Let's dance
|
静态方法
Java8开始使用静态方法
格式:
1
2
3
|
public static 返回值类型 方法名称(参数列表){
//方法体
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public interface MyInterfaceStatic{
public static void methodStatic(){
System.out.println("static successed");
}
}
public class MyInterfaceimpl implements MyInterfaceStatic{
@override
public void methodAbs(){
System.out.println("111");
}
}
|
1
2
3
4
5
6
7
8
9
10
|
public class Demo{
public static void main(String[] args){
MyInterfaceimpl impl = new MyInterfaceimpl();
impl.methodStatic();// wrong
MyInterfaceStatic.methodStatic;//right
}
}
//results: 111
// Let's dance
|
私有方法
问题描述:我们需要抽取一个公用方法来解决两个默认方法直接重复代码问题,但这个共有方法不应该让实现类使用,应该是私有化的
解决方案
从Java 9 开始,接口允许定义私有方法
- 普通私有方法,解决多个默认方法之间重复代码问题
1
2
3
|
private 返回值类型 方法名称(参数列表){
//...
}
|
- 静态私有方法,解决多个静态方法之间重复代码问题
1
2
3
|
private static 返回值类型 方法名称(参数列表){
//...
}
|
普通私有方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public interface MyInterface{
public default void methodDefult1(){
System.out.println("Let's dance1");
methodCommon();
}
public default void methodDefult2(){
System.out.println("Let's dance2");
methodCommon();
}
private void method(){
System.out.println("Let's have sex");
System.out.println("Let's have sex");
System.out.println("Let's have sex");
}
}
|
静态私有方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public interface MyInterface{
public static void methodStatic1(){
System.out.println("Let's dance1");
methodCommon();
}
public static void methodStatic2(){
System.out.println("Let's dance2");
methodCommon();
}
private static void method(){
System.out.println("Let's have sex");
System.out.println("Let's have sex");
System.out.println("Let's have sex");
}
}
|
1
2
3
4
5
6
7
8
|
public class Demo{
public static void main(String[] args){
impl.methodStatic1();// wrong
MyInterfaceStatic.methodStatic1;//right
}
}
//results: 111
// Let's dance
|
接口的常量
接口中也可以定义“成员变量”,但是必须使用 private, static, final 三个关键字进行修饰
从效果上看,这就是接口的常量
格式:
public static final 数据类型 常量名称 = 值;
一旦使用final,说明数据不可变
注意事项:
- 接口当中的常量,可以省略三个关键字
- 必须赋值
- 常量名称使用大写,下划线
1
2
3
|
public interface MyInterfaceConst{
public static final int NUM = 2;//必须赋值
}
|
1
2
3
4
5
6
7
|
public class Demo{
public static void main(String[] args){
impl.methodStatic1();// wrong
System.out.println(MyInterfaceConst.NUM);//right
}
}
//result is 2
|
继承父类并实现多个接口
- 接口没有静态代码块或构造方法的
- 一个类的直接父类是唯一的,但是一个类可以实现多个接口
格式:
1
2
3
|
public class MyInterfaceImpl implements MyInterfaceA, MyInterfaceB{
//覆盖重写所有抽象方法
}
|
- 如果实现类所实现的多个接口中有重复抽象方法,只需覆盖重写一次
- 如果实现类没有覆盖重写所有抽象方法,那么该类为抽象类
- 如果实现类所实现的多个接口中,存在重复的默认方法,那么实现类必须对默认方法进行覆盖重写
- 一个类如果他的直接父类中的方法和接口中的方法产生冲突,父类方法优先
接口之间可以多继承
1
2
3
|
public interface MyInterface extends MyInterfaceA, MyInterfaceB{
//...
}
|