2012-11-23 81 views
0

嘿所以我需要設計一個程序,可以添加一個公司的皮卡或交付的列表爲我的課程作業,我做了一個列表名爲訪問如下圖所示。如何使用只有一個列表

我想知道如何將每個拾取或遞送添加到列表中,以便可以區分最初是什麼,以便我可以僅顯示拾取或僅顯示遞送。

class List 
{ 
/* 
* This object represents the List. It has a 1:M relationship with the Visit class 
*/ 

    private List<Visits> visits = new List<Visits>(); 
    //List object use to implement the relationshio with Visits 

    public void addVisits(Visits vis) 
    { 
     //Add a new Visit to the List 
     visits.Add(vis); 
    } 

    public List<String> listVisits() 
    {//Generate a list of String objects, each one of which represents a Visit in List. 

     List<String> listVisits = new List<string>(); 
     //This list object will be populated with Strings representing the Visits in the lists 

     foreach (Visits vis in visits) 
     { 
      String visAsString = vis.ToString(); 
      //Get a string representing the current visit object 

      listVisits.Add(visAsString); 
      //Add the visit object to the List 
     } 

     return listVisits; 
     //Return the list of strings 
    } 

    public Visits getVisits(int index) 
    { 
     //Return the visit object at the <index> place in the list 

     int count = 0; 
     foreach (Visits vis in visits) 
     { 
      //Go through all the visit objects 
      if (index == count) 
       //If we're at the correct point in the list... 
       return vis; 
      //exit this method and return the current visit 
      count++; 
      //Keep counting 
     } 
     return null; 
     //Return null if an index was entered that could not be found 
    } 

SHOW CODE

 /* 
     * Update the list on this form the reflect the visits in the list 
     */ 
     lstVisits.Items.Clear(); 
     //Clear all the existing visits from the list 

     List<String> listOfVis = theList.listVisits(); 
     //Get a list of strings to display in the list box 


     lstVisits.Items.AddRange(listOfVis.ToArray()); 
     //Add the strings to the listBox. Note to add a list of strings in one go we have to 
     //use AddRange and we have to use the ToArray() method of the list that we are adding 
+0

通常情況下,您將有兩個列表,一個用於拾取,另一個用於交付。當你不需要區分不同類型的訪問時,只有一個「訪問」列表是有意義的(即,即使這些方法在每個具體類型中都有不同的(多態)實現,你只需觸及Visit類中的東西)。 – Cameron

+0

好的在這種情況下,我有一個交貨按鈕和一個皮卡,它們將它們添加到列表框中以顯示摘要...我如何實現一個按鈕來排序它們,如果僅顯示皮卡按鈕或僅交付 – TAM

回答

1

看起來你同時擁有PickupDelivery繼承稱爲Visits*常見的類類型。

阿爾特Visits提供一種方法來區分各種訪問,例如,像這樣:

enum VisitKind { 
    Pickup, 
    Delivery 
} 

public class Visits { 
    public VisitKind KindOfVisit {get;private set;} 
    protected Visits(VisitKind kind) { 
     KindOfVisit = kind; 
    } 
} 

現在修改的PickupDelivery建設者正確的那種傳遞給他們的父類的構造函數,像這樣:

public class Pickup : Visits { 
    public Pickup() : base(VisitKind.Pickup) {} 
} 
public class Delivery : Visits { 
    public Delivery() : base(VisitKind.Delivery) {} 
} 

現在你可以檢查常見的KindOfVisit財產PickupDelivery爲了告訴那種訴ISIT在運行時:

public IList<Delivery> GetDeliveries() { 
    var res = new List<Delivery>(); 
    foreach (var v in visits) { 
     if (v.KindOfVisit == VisitKind.Delivery) { 
      res.Add((Delivery)v); 
     } 
    } 
    return res; 
} 

該解決方案是不理想的,因爲當有人增加了一個新的訪問,認爲訪問處理將繼續編制和往常一樣的代碼。但是,解決此問題比添加屬性困難得多:一種解決方案是採用Visitor Pattern,它比簡單的Kind屬性更好,但它有其自身的侷限性。


*命名與名詞的複數,一類是標新立異 - Visit可能會是一個更好的名字。

+0

好吧,但是上面的代碼只能顯示說皮卡的列表嗎? 我真的沒有那部分 – TAM

+0

@TAM你是否已經從評論中找出問題的答案? – dasblinkenlight

+0

不,但我會將我當前的展示代碼添加到原始帖子中,然後它會僅僅是.visitkind =發送還是其他內容? – TAM

0

在訪問類跟蹤如果它是一個拾取器或遞送,添加一個參數,並且在訪問類添加的getType()/的setType()方法檢索這是

相關問題