我正在嘗試爲ASP.NET MVC2中的模型類創建一個接口,並且我想知道是否可以在另一個接口中使用List<interface>
。如果我給出一個代碼示例,那會更好。在另一個接口C#中使用列表<interface>#
我有兩個接口,一個終端可以有多個托架。所以我編寫我的接口,如下所示。
灣接口:
public interface IBay
{
// Properties
int id {get; set;}
string name {get;set;}
// ... other properties
}
終端接口:
public interface ITerminal
{
// Properties
int id {get;set;}
string name {get;set;}
// ... other properties
List<IBay> bays {get;set;}
}
我的問題是,當我基於這些接口我如何建立海灣列表實現我的課。我將不得不在ITerminal界面之外以及具體實現內部執行托架列表嗎?
我的目標是能夠做到以下幾點:
具體實現:
灣類別:
class Bay : IBay
{
// Constructor
public Bay()
{
// ... constructor
}
}
終端類:
class Terminal : ITerminal
{
// Constructor
public Terminal()
{
// ... constructor
}
}
然後能夠訪問這樣的海灣清單
Terminal.Bays
任何幫助/建議將不勝感激。
你的代碼看起來很好,除了一般的設計建議,海灣應該是'IList'。有關它不起作用? –
2012-02-14 19:16:30
我不確定我是否理解對不起。 ITerminal接口意味着您需要將'Bays'訪問器添加到您的終端對象,然後您可以創建'List'並向其添加'Bay'對象,然後從訪問器返回它。或者你的意思是如何設置它,以便你可以返回一個'List '作爲'List '? –
Rup
2012-02-14 19:18:33
@ChrisShain:這更像是一個結構問題和最佳實踐。 – Gnex 2012-02-14 21:50:05