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
    }

显示代码

       /*
         * 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
4

2 回答 2

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以便在运行时判断访问的类型:

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;
}

这种解决方案并不理想,因为当有人添加一种新的访问时,处理访问的代码会照常继续编译。不过,这个问题的解决方案比添加属性要困难得多:一种解决方案是使用比简单属性更好的访问者模式Kind,但它有其自身的局限性。


*用名词的复数来命名一个类是非常规的——Visit可能是一个更好的名字。

于 2012-11-23T21:16:06.250 回答
0

在您的 Visits 类中添加一个参数来跟踪它是取货还是交付,并在 Visits 类中添加一个 getType() /setType() 方法来检索它是哪种类型

于 2012-11-23T21:11:46.590 回答