簡體   English   中英

無法將接口的鏈表實例化為實現該接口的類的鏈表

[英]Can't instantiate a linkedlist of an interface as a linkedlist of a class that implements the interface

我有:

LinkedList<Interface> fred = new LinkedList<Class>();

其中Interface和Class分別是接口和類的名稱,但是java給我一個類型不匹配錯誤。 我的課程實現了該接口,並成功包含了該接口所要求的所有功能。

為什么這不起作用?

你可以這樣做:

LinkedList<MyInterface> fred = new LinkedList<>();

推薦的方法是:

List<MyInterface> fred = new LinkedList<>();

您正在尋找:

LinkedList<Interface> fred = new LinkedList<>();

以下內容無效,因為T的類型在左側和右側必須相同,顯然不是這種情況。

LinkedList<Interface> fred = new LinkedList<Class>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM