Java 8 介面的預設方法和引入的多繼承
以前經常談論的Java對比c++的一個優勢是Java中沒有多繼承的問題。 因為Java中子類只能繼承(extends
)單個父類, 儘管可以實現(implements)多個介面,但是介面中只有抽象方法,方法體是空的,沒有具體的方法實現,不會有方法衝突的問題。
這些都是久遠的說法了,自從今年Java 8釋出後,介面中也可以定義方法了(default method
)。 之所以打破以前的設計在介面中
增加具體的方法, 是為了既有的成千上萬的Java類庫的類增加新的功能, 且不必對這些類重新進行設計。 比如, 只需在Collection
介面中
增加 default Stream<E> stream()
這是一個折衷的設計,帶來的問題就是為Java引入了多繼承的問題。 我們知道,介面可以繼承介面, 類可以繼承類和實現介面。 一旦繼承的類和實現的介面中有
相同簽名的方法,會出現什麼樣的狀況呢?
本文將探討各種情況的多繼承,以便能清楚的理解Java多繼承的規則。
1.介面繼承多個父介面
假定有三個介面Interface A
,Interface B
,Interface C
,繼承關係如下:
+---------------+ +------------+
| Interface A | |Interface B |
+-----------^---+ +---^--------+
| |
| |
| |
+-+------------+--+
| Interface C|
+------------+
複製程式碼
A,B擁有相同簽名的預設方法default String say(String name)
,如果介面C沒有override這個方法, 則編譯出錯。
interface A {
default String say(String name) {
return "hello " + name;
}
}
interface B {
default String say(String name) {
return "hi," + name;
}
}
interface C extends A,B{
}
複製程式碼
編譯直接報錯:
hxr-mac:interfacemutil houxiurong$ javac -J-Duser.country=US MultipleInheritance.java
MultipleInheritance.java:23 : error: interface C inherits unrelated defaults for say(String) from types A and B
interface C extends A,B{
^
1 error
hxr-mac:interfacemutil houxiurong$ javac MultipleInheritance.java
MultipleInheritance.java:23: 錯誤: 介面 C從型別 A 和 B 中繼承了say(String) 的不相關預設值
interface C extends A,B{
^
1 個錯誤
複製程式碼
介面C提醒必須要override say(String name)
方法才編譯通過。
我們可以在子介面C中覆蓋override這個方法,這樣編譯就不會出錯了:
interface C extends A,B {
@Override
default String say(String name) {
return "greet" + name;
}
}
複製程式碼
注意方法簽名不包括方法的返回值, 也就是僅僅返回值不同的兩個方法的簽名也是相同的。 下面的程式碼編譯不會出錯,因為A和B的預設方法不同, C隱式繼承了兩個預設方法。
interface A {
default void say(int name) {
}
}
interface B {
default void say(String name) {
}
}
interface C extends A,B{
}
複製程式碼
但是有的情況下即使是不同簽名的方法也是很難分辨的:
interface A {
default void say(int name) {
System.out.println("A=" + name);
}
}
interface B {
default String say(short name) {
System.out.println("B=" + name);
return "hi," + name;
}
}
interface C extends A,B {
default String say(String name) {
System.out.println("C=" + name);
return "hi," + name;
}
}
static class D implements C {
}
public static void main(String[] args) {
D d = new D();
byte a = 1;
d.say(a);
d.say("hoxiurong");
}
複製程式碼
執行結果如下:
B=1
C=hoxiurong
複製程式碼
Java會選擇最適合的方法, 請參看Oracle官網Java規範15.12.2.5
2.介面多層繼承
下面看一下多層繼承的問題。 繼承關係如下圖, A2繼承A1,C繼承A2。
+---------------+
| Interface A1 |
+--------+------+
|
|
+--------+------+
| Interface A2 |
+-------+-------+
|
|
+-------+--------+
| Interface C |
+----------------+
複製程式碼
基於我們以前對類繼承的認識,很容易知道C會繼承A2的預設方法,包括直接定義的預設方法, 覆蓋的預設方法,以及隱式繼承於A1介面的預設方法。
interface A {
default void say(int a) {
System.out.println("A");
}
default void run() {
System.out.println("A.run");
}
}
interface B extends A {
default void say(int a) {
System.out.println("B");
}
default void play() {
System.out.println("B.play");
}
}
interface C extends A,B {
}
static class D implements C {
}
public static void main(String[] args) {
D d = new D();
d.run();
d.play();
}
複製程式碼
3.多層多繼承
上面一個例子還是單繼承的例子,如果如下圖的多繼承呢?
+---------------+
| Interface A1 |
+--------+------+
|
|
|
+--------+------+ +---------------+
| Interface A2 | | Interface B |
+-----------+---+ +--+------------+
| |
| |
| |
+-------+-------++
| Interface C |
+----------------+
複製程式碼
如果A2和B擁有相同簽名的方法,這和第一個例子一樣。 如果不想編譯出錯,可以覆蓋父介面的預設方法,還可以呼叫指定父介面的預設方法:
interface A1 {
default void sayHello(int a) {
System.out.println("A1");
}
}
interface A2 extends A1 {
}
interface B {
default void sayHello(int a) {
System.out.println("B");
}
}
interface C extends A2,B {
default void sayHello(int a) {
B.super.sayHello(a);
}
}
複製程式碼
4.更復雜的多層多繼承
+--------------+
| Interface A1 |^+-----+
+------+------++ |
| |
| |
+-------+-------+ |
| Interface A2 | |
+------------+--+ |
^---+ |
| |
+--+------+-----+
| Interface C |
+---------------+
複製程式碼
介面A2繼承A1, 介面C繼承A2和A1。 程式碼如下,
interface A1 {
default void say() {
System.out.println("hello,A1");
}
}
interface A2 extends A1 {
@Override
default void say() {
System.out.println("hello,A2");
}
}
interface C extends A2,A1 {
}
static class D implements C {
}
public static void main(String[] args) {
D d = new D();
d.say();
}
複製程式碼
執行程式輸出如下:
hello,A2
Process finished with exit code 0
複製程式碼
以上程式碼不會編譯出錯,執行程式輸出hello,A2
。
可以看到介面C會隱式繼承子介面的方法, 也就是子介面A2的預設方法。
5.類繼承
如果繼承關係型別全部是類, 那麼由於類依然是單繼承的, 不會有多繼承的問題。
6.類和介面混雜
我們把第一個例子中的其中一個介面換成類,會出現什麼現象呢。
+-------------+ +-----------+
| Interface A | | Class B |
+-----------+-+ +-+---------+
| |
| |
+---+----+-+
| Class C |
+----------+
複製程式碼
以下程式碼不會編譯出錯:
interface A {
default void say() {
System.out.println("hello,A");
}
}
static class B {
public void say() {
System.out.println("hello,B");
}
}
static class C extends B implements A {
}
public static void main(String[] args) {
C c = new C();
c.say(); //hello,B
}
複製程式碼
結果輸出hello,B
。
可以看出,子類優先繼承父類的方法
, 如果父類沒有相同簽名的方法,才繼承介面的預設方法。
7.總結
更復雜的繼承關係可以簡化成以上的繼承關係。 根據以上的例子, 可以得出以下的結論:
^1.類優先於介面。 如果一個子類繼承的父類和介面有相同的方法實現。 那麼子類繼承父類的方法。
^2.子型別中的方法優先於父型別中的方法。
^3.如果以上條件都不滿足,則必須顯示覆蓋override
或實現implements
其方法,或者宣告成abstract
。
文章主體思路轉載自:鳥窩