一、接口的概述
- 接口介绍:当一个类中所有方法都是抽象方法的时候,我们就可以将其定义为接口
- Java中接口存在的两个意义:1、规则的定义;2、程序的扩展性
- 接口的定义:
public interface 接口名 {}
- 类实现接口:
public class 类名 implements 接口名 {}
- 接口的特点:接口不能实例化,可以创建接口的实现类对象来使用
- 接口的子类:要么重写接口中的所有抽象方法;要么子类也是抽象类
- 接口与类的关系:可以单实现,也可以多实现
public interface Inter {
public abstract void study();
}
public interface InterA {
public abstract void print1();
public abstract void print2();
public abstract void study();
}
public class InterImpl implements Inter , InterA {
@Override
public void study() {
System.out.println("我是实现类中的study方法");
}
@Override
public void print1() {
}
@Override
public void print2() {
}
}
public class Test1Interface {
public static void main(String[] args) {
// Inter i = new Inter();
InterImpl ii = new InterImpl();
ii.study();
}
}
二、接口中的成员特点
- 成员变量:只能是常量,系统会默认加入三个关键字
public static final
- 构造方法:接口没有构造方法
public InterImpl(){super();}
:这个super访问的是父类Object的构造方法
- 成员方法:只能是抽象方法,统会默认加入两个关键字
public abstract
public interface Inter {
public static final int NUM = 10;
// public Inter(){}
public abstract void show();
}
public class TestInterface {
public static void main(String[] args) {
System.out.println(Inter.NUM);
}
}
class InterImpl extends Object implements Inter{
public InterImpl(){
super();
}
public void method(){
// NUM = 20;
System.out.println(NUM);
}
public void show(){
}
}
三、JDK8版本中接口成员的特点
1、默认方法
- 默认方法:JDK8版本后,允许在接口中定义非抽象方法,但是需要使用关键字default修饰,这些方法就是默认方法
- 作用:解决接口升级的问题,能够实现丰富接口的同时,不需要修改子类的代码
- 定义格式:
public default void show() {}
- 多实现存在相同的默认方法:必须重写默认方法,否则编译错误
public interface InterA {
public default void show(){
System.out.println("我是A接口中的show方法");
}
}
public interface InterB {
public default void show(){
System.out.println("B....show方法");
}
}
public class TestInterface {
public static void main(String[] args) {
InterAImpl ia = new InterAImpl();
ia.show();
}
}
class InterAImpl implements InterA, InterB {
@Override
public void show() {
}
}
2、static静态方法
- static静态方法:JDK8版本后,接口中允许定义static静态方法
- 定义格式:
public static void show() {}
- 调用要求:只能通过
接口名.方法名()
来调用,不能通过实现类名或者对象名进行调用InterA.show();
和InterB.show();
不会冲突,因为只能使用接口名来调用接口中的static静态方法
public interface InterA {
public static void show(){
System.out.println("InterA...show");
}
}
public interface InterB {
public static void show(){
System.out.println("InterB...show");
}
}
public class TestInterface {
public static void main(String[] args) {
InterA.show();
InterB.show();
}
}
class InterAImpl implements InterA , InterB {
}
四、JDK9中接口成员的特点
- 接口中的私有方法:JDK9版本后,可以在接口中定义私有的成员方法
- 作用:提取接口中公共的方法
- 格式:
private void show() {}
private static void method() {}
public interface Inter {
public default void start() {
System.out.println("start方法执行了...");
log();
}
public default void end() {
System.out.println("end方法执行了...");
log();
}
private void log(){
System.out.println("日志记录 ( 模拟 )");
}
//--------------------------------------
public static void open() {
check();
System.out.println("open方法执行了");
}
public static void close(){
check();
System.out.println("close方法执行了");
}
private static void check(){
System.out.println("权限校验 ( 模拟 )");
}
}
public class TestInterface {
public static void main(String[] args) {
InterImpl ii = new InterImpl();
ii.start();
ii.end();
Inter.open();
Inter.close();
}
}
class InterImpl implements Inter {
}
五、接口使用思路总结
- 如果发现一个类中所有的方法都是抽象方法,那么就可以将该类,改进为一个接口
- 涉及到了大面积更新方法,而不想去修改每一个实现类,就可以将更新的方法,定义为带有方法体的默认方法
- 希望默认方法调用的更加简洁,可以考虑涉及为static静态方法(需要去掉default关键字)
- 默认方法种出现了重复的代码,可以考虑抽取出一个私有方法(需要去掉default关键字)
六、类和接口的关系
- 类与类的关系:继承关系,只能单继承,但是可以多层继承
- 类与接口的关系:实现关系,可以单实现,也可以多实现,还可以在继承一个类的同时实现多个接口
- 接口与接口的关系:继承关系,可以单继承,也可以多继承
如果直接父类, 和接口中出现了相同的方法声明, 但是代码逻辑不一样,优先使用直接父类的代码逻辑
public class Fu {
public void show(){
System.out.println("Fu...show");
}
}
public interface Inter {
public default void show(){
System.out.println("Inter....show");
}
}
public class TestInterface {
public static void main(String[] args) {
InterImpl ii = new InterImpl();
ii.show();// Fu...show
}
}
class InterImpl extends Fu implements Inter {
}