天天看點

java接口新版本新特性_Java新特性-接口的新特性

接口的組成和更新的演變概述

常量

public static final

抽象方法

public abstract

預設方法(Java 8)

靜态方法(Java 8)

私有方法(Java 9)

預設方法(Java 8)

作用:可以實作代碼的更新, 好處就是不會破壞現在已有的代碼,下面我将給出一個示例進行體驗一下吧,建立一個接口 MyInterface

public interface MyInterface {

void showOne();

void showTwo();

}

緊接着建立兩個實作類如下,分别為 MyInterfaceImplOne 與 MyInterfaceImplTwo

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");

}

}

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");

}

}

在以前的版本中, 如果接口當中想要添加新的方法, 兩個實作類就必須要實作新添加的方法,如果想要解決接口新增了一個方法實作類必須實作的問題,可以使用繼承的方式進行擴充來解決

public interface MyInterfaceSon extends MyInterface {

void showThree();

}

這個時候呢,如果那個實作需要實作這個新增的方法隻需要把實作的接口換一下即可進行實作對應的方法了,如下假如我的 MyInterfaceImplOne 這個實作類需要那個新增的方法我就修改了這個類具體内容如下

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接口新版本新特性_Java新特性-接口的新特性

除了以上的方式可以解決,其實還可以使用接口中的預設方法來解決,具體玩法如下所示,先不做過多的解釋先看

public interface MyInterface {

void showOne();

void showTwo();

default void showThree() {

System.out.println("Show Three");

}

}

修改了接口添加了預設方法之後在來看看 MyInterfaceImplOne 這個實作類吧,我之前說了這個實作類我需要實作新增的方法,那麼我就修改了這個實作類,另外的那個實作類不需要我就沒動,一樣的解決了如上的問題,MyInterfaceImplOne 具體的内容還是和上方的一樣我這裡隻貼圖了不貼代碼了就

java接口新版本新特性_Java新特性-接口的新特性

靜态方法(Java 8)

編寫格式如下

public static 傳回值類型 方法名(參數清單){}

注意事項

靜态方法隻能通過接口名調用,不能通過實作類名或者對象名調用,下面我将給出一個示例進行參考

首先建立一個 TestInterfaceOne 接口

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 接口

public interface TestInterfaceTwo {

static void test() {

System.out.println("TestInterfaceTwo In The Static Method Run");

}

}

然後在建立一個實作類

public class TestImpl implements TestInterfaceOne, TestInterfaceTwo {

@Override

public void show() {

System.out.println("Show Method Run!");

}

}

開始使用,按照多态的方式建立對象并使用

public class TestMain {

public static void main(String[] args) {

TestInterfaceOne interfaceOne = new TestImpl();

interfaceOne.show();

interfaceOne.method();

}

}

隻能使用接口名來調用靜态方法, 不能通過實作類名或者對象名調用靜态方法

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 接口

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新特性-接口的新特性

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 實作類具體内容如下

public class TestInterfaceImpl implements TestInterface {

}

建立好了之後開始使用

public class TestMain {

public static void main(String[] args) {

TestInterfaceImpl testInterface = new TestInterfaceImpl();

testInterface.showOne();

TestInterface.staticMethod();

}

}