1. 程式人生 > >關於介面的多繼承的理解

關於介面的多繼承的理解

在:http://www.runoob.com/java/java-intro.html上看到以下這段話:

Java語言是面向物件的:Java語言提供類、介面和繼承等原語,為了簡單起見,只支援類之間的單繼承,但支援介面之間的多繼承,並支援類與介面之間的實現機制(關鍵字為implements)

對於類之間的單繼承我們很好理解,是為了避免子類被引用的時候同一個方法無法判斷應該使用哪個父類的方法,如類One同時繼承了類Two和類Three,類Two和類Three中都有printStr方法,當呼叫pringStr方法的時候,就無法選擇使用哪個類的,編譯器直接報錯!可以看以下例子:

public class Two implements InterfaceTwo,InterfaceThree{
	public void printStr(){
		System.out.println("Print by Class Two");
	}

	@Override
	public void printStrByInterface() {
		// TODO 自動生成的方法存根
		System.out.println("Print by IntercaseTwo Two");
	}
}
public class Three {
    public void printStr(){
        System.out.println("Print by Class Three");
    }
}
//編譯器報錯error:標記“,”上有語法錯誤,應為 .
public class One extends Two,Three {

}

所以對於類的繼承extends來說只能是單繼承的。

但是介面之間確實多繼承的,難道介面沒有上面提到的類繼承的那些問題嗎?這裡需要重溫一下介面的特點,如知識點二。介面都是抽象的,他下面的方法也必須全部是抽象方法,沒有任何的具體實現,即使繼承的兩個父介面包含同樣的方法也沒有任何影響,可以看下面的例子:InterfaceOne繼承了InterfaceTwo和InterfaceThree,同時InterfaceTwo和InterfaceThree中都定義了抽象方法printStrByInterface:

package interfaceMutilInherit;

public interface InterfaceOne extends InterfaceTwo,InterfaceThree{
	
}
package interfaceMutilInherit;

public interface InterfaceTwo {
	public void printStrByInterface();
}
package interfaceMutilInherit;

public interface InterfaceThree {
	public void printStrByInterface();
}
以上程式碼完全沒有問題,正常編譯通過。

接下來看看測試例子,測試介面以及把Three類實現InterfaceThree。完整測試

//類One整合類Two
public class One extends Two{

}

//類Two實現介面InterfaceTwo和InterfaceThree
public class Two implements InterfaceTwo,InterfaceThree{
	public void printStr(){
		System.out.println("Print by Class Two");
	}

	/* (非 Javadoc)
	 * @see interfaceMutilInherit.InterfaceTwo#printStrByInterface()
	 */
	@Override
	public void printStrByInterface() {
		// TODO 自動生成的方法存根
		System.out.println("Print by IntercaseTwo Two");
	}
}

//類Three僅實現介面InterfaceThree
public class Three implements InterfaceThree{
	public void printStr(){
		System.out.println("Print by Class Three");
	}

	/* (非 Javadoc)
	 * @see interfaceMutilInherit.InterfaceThree#printStrByInterface()
	 */
	@Override
	public void printStrByInterface() {
		// TODO 自動生成的方法存根
		System.out.println("Print by Interface Three");
	}
}

//介面InterfaceTwo
public interface InterfaceTwo {
	public void printStrByInterface();
}
//介面InterfaceThree
public interface InterfaceThree {
	public void printStrByInterface();
}


//測試類TestOneTwoThree
public class TestOneTwoThree {
	public static void main(String[] args){
		One one = new One();
		one.printStr();
		InterfaceTwo inter = new Two();
		inter.printStrByInterface();
		InterfaceThree inter3 = new Three();
		inter3.printStrByInterface();
	}
}
得到的結果是:

Print by Class Two
Print by IntercaseTwo Two
Print by Interface Three


知識點一:

介面的特點

1、Java介面中的成員變數預設都是public,static,final型別的(都可省略),必須被顯示初始化,即介面中的成員變數為常量(大寫,單詞之間用"_"分隔)
2、Java介面中的方法預設都是public,abstract型別的(都可省略),沒有方法體,不能被例項化
3、Java介面中只能包含public,static,final型別的成員變數和public,abstract型別的成員方法
4、介面中沒有構造方法,不能被例項化
5、一個介面不能實現(implements)另一個介面,但它可以繼承多個其它的介面
6、Java介面必須通過類來實現它的抽象方法
7、當類實現了某個Java介面時,它必須實現介面中的所有抽象方法,否則這個類必須宣告為抽象類
8、不允許建立介面的例項(例項化),但允許定義介面型別的引用變數,該引用變數引用實現了這個介面的類的例項
9、一個類只能繼承一個直接的父類,但可以實現多個介面,間接的實現了多繼承

知識點二:

關於介面實現類的初始化,一般介面xxinterface,他的實現類xximpl,要用實現類不是xximpl xx=new xximpl(),而是xxinterface xx=new xximpl(),好比例子中

public class TestOneTwoThree {
    public static void main(String[] args){
        One one = new One();
        one.printStr();
        InterfaceTwo inter = new Two();
        inter.printStrByInterface();
    }
}


參考:

介面的特點:http://blog.csdn.net/liujun13579/article/details/7736116/