0
点赞
收藏
分享

微信扫一扫

Java新特性-接口的新特性

接口的组成和更新的演变概述

常量

public static final

抽象方法

public abstract
  • 默认方法(Java 8)
  • 静态方法(Java 8)
  • 私有方法(Java 9)

默认方法(Java 8)

作用:可以实现代码的升级, 好处就是不会破坏现在已有的代码,下面我将给出一个示例进行体验一下吧,新建一个接口 ​​MyInterface​

/**
* @author BNTang
*/
public interface MyInterface {

void showOne();

void showTwo();

}

紧接着创建两个实现类如下,分别为 ​​MyInterfaceImplOne​​​ 与 ​​MyInterfaceImplTwo​

/**
* @author BNTang
*/
public class MyInterfaceImplOne implements MyInterface {
@Override
public void showOne() {
System.out.println("MyInterfaceImplOne One Show");
}

@Override
public void showTwo() {
System.out.println("MyInterfaceImplOne Two Show");
}
}
/**
* @author BNTang
*/
public class MyInterfaceImplTwo implements MyInterface {
@Override
public void showOne() {
System.out.println("MyInterfaceImplTwo One Show");
}

@Override
public void showTwo() {
System.out.println("MyInterfaceImplTwo Two Show");
}
}

在以前的版本中, 如果接口当中想要添加新的方法, 两个实现类就必须要实现新添加的方法,如果想要解决接口新增了一个方法实现类必须实现的问题,可以使用继承的方式进行扩展来解决

/**
* @author BNTang
*/
public interface MyInterfaceSon extends MyInterface {
void showThree();
}

这个时候呢,如果那个实现需要实现这个新增的方法只需要把实现的接口换一下即可进行实现对应的方法了,如下假如我的 ​​MyInterfaceImplOne​​ 这个实现类需要那个新增的方法我就修改了这个类具体内容如下

/**
* @author BNTang
*/
public class MyInterfaceImplOne implements MyInterfaceSon {
@Override
public void showOne() {
System.out.println("MyInterfaceImplOne One Show");
}

@Override
public void showTwo() {
System.out.println("MyInterfaceImplOne Two Show");
}

@Override
public void showThree() {
System.out.println("MyInterfaceImplOne Three Show");
}
}

Java新特性-接口的新特性_JavaEE

除了以上的方式可以解决,其实还可以使用接口中的默认方法来解决,具体玩法如下所示,先不做过多的解释先看

/**
* @author BNTang
*/
public interface MyInterface {

void showOne();

void showTwo();

default void showThree() {
System.out.println("Show Three");
}
}

修改了接口添加了默认方法之后在来看看 ​​MyInterfaceImplOne​​​ 这个实现类吧,我之前说了这个实现类我需要实现新增的方法,那么我就修改了这个实现类,另外的那个实现类不需要我就没动,一样的解决了如上的问题,​​MyInterfaceImplOne​​ 具体的内容还是和上方的一样我这里只贴图了不贴代码了就

Java新特性-接口的新特性_静态方法_02

静态方法(Java 8)

编写格式如下

public static 返回值类型 方法名(参数列表){}

注意事项

静态方法只能通过接口名调用,不能通过实现类名或者对象名调用,下面我将给出一个示例进行参考

首先新建一个 ​​TestInterfaceOne​​ 接口

/**
* @author BNTang
*/
public interface TestInterfaceOne {

void show();

default void method() {
System.out.println("TestInterfaceOne In The Default Method run");
}

static void test() {
System.out.println("TestInterfaceOne In The Static Method run");
}

}

紧接着在新建一个 ​​TestInterfaceTwo​​ 接口

/**
* @author BNTang
*/
public interface TestInterfaceTwo {

static void test() {
System.out.println("TestInterfaceTwo In The Static Method Run");
}

}

然后在新建一个实现类

/**
* @author BNTang
*/
public class TestImpl implements TestInterfaceOne, TestInterfaceTwo {
@Override
public void show() {
System.out.println("Show Method Run!");
}
}

开始使用,按照多态的方式创建对象并使用

/**
* @author BNTang
*/
public class TestMain {
public static void main(String[] args) {
TestInterfaceOne interfaceOne = new TestImpl();
interfaceOne.show();
interfaceOne.method();
}
}

只能使用接口名来调用静态方法, 不能通过实现类名或者对象名调用静态方法

/**
* @author BNTang
*/
public class TestMain {
public static void main(String[] args) {
TestInterfaceOne interfaceOne = new TestImpl();
interfaceOne.show();
interfaceOne.method();

TestInterfaceOne.test();
// interfaceOne.test();

TestInterfaceTwo.test();
}
}

私有方法(Java 9)

作用:Java 8允许在接口中定义带方法体的默认方法和静态方法,可能就会引发一个问题:当两个默认方法或者静态方法中包含一段相同的代码实现时,程序必然要考虑将这段实现代码抽取成一个共性方法,共性方法是不需要让别人使用的,因此用私有给隐藏起来,这就是 Java 9 增加私有方法的必然性

注意事项

  • 默认方法可以调用私有的静态方法和私有的非静态方法
  • 静态方法只能调用私有的静态方法

下面给出一段小小的示例供参考

新建一个 ​​TestInterface​​ 接口

/**
* @author BNTang
*/
public interface TestInterface {

default void showOne() {
System.out.println("showOne 开始执行");
System.out.println("showOne 结束执行");
}

static void staticMethod() {
System.out.println("staticMethod 开始执行");
System.out.println("staticMethod 结束执行");
}

private void show() {
System.out.println("abc");
System.out.println("abc");
System.out.println("abc");
}

private static void method() {
System.out.println("abc");
System.out.println("abc");
System.out.println("abc");
}
}

如上接口中的方法分别的解释如下

  • showOne 默认方法
  • staticMethod 静态方法
  • show 私有方法
  • method 私有静态方法

接下来我们就开始分别在静态方法和默认方法中开始调用私有静态方法和私有方法看看效果吧,如下

Java新特性-接口的新特性_Java 8_03

/**
* @author BNTang
*/
public interface TestInterface {

default void showOne() {
System.out.println("showOne 开始执行");
// 能
show();
// 能
method();
System.out.println("showOne 结束执行");
}

static void staticMethod() {
System.out.println("staticMethod 开始执行");
// 不能调用
// show();
method();
System.out.println("staticMethod 结束执行");
}

private void show() {
System.out.println("abc");
System.out.println("abc");
System.out.println("abc");
}

private static void method() {
System.out.println("abc");
System.out.println("abc");
System.out.println("abc");
}
}

如上图可以看出静态方法是不能调用非静态的私有方法的,紧接着创建一个 ​​TestInterfaceImpl​​ 实现类具体内容如下

/**
* @author BNTang
*/
public class TestInterfaceImpl implements TestInterface {
}

创建好了之后开始使用

/**
* @author BNTang
*/
public class TestMain {

public static void main(String[] args) {
TestInterfaceImpl testInterface = new TestInterfaceImpl();

testInterface.showOne();
TestInterface.staticMethod();
}

}




举报

相关推荐

0 条评论