簡體   English   中英

創建繼承通用默認參數的Collection類

[英]Create Collection class which inherits generic default parameters

我想在存儲庫外部創建一個C#類,該類繼承了所有默認的通用方法(添加,全部刪除等)。 以下代碼有效。 我的目標是將List ShoppingCart移至存儲庫之外。

public class CartLine
{
    public int CartLineId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
}

以下代碼工作:

 public class ShoppingCartRepository
    {

        private List<CartLine> ShoppingCart = new List<CartLine>();


        public IEnumerable GetShoppingCart()
        {
            return ShoppingCart.ToList();
        }

        public virtual void AddItem(int productid, int quantity)
        {
            ShoppingCart.Add(new CartLine { ProductId = productid, Quantity = quantity });
        }

        public virtual void RemoveItem(int cartlineid)
        {
            ShoppingCart.RemoveAll(l => l.CartLineId == cartlineid);
        }

該代碼不起作用: “錯誤:購物車不包含ToList的定義。”

public class ShoppingCart : List<ShoppingCart>
{
    public ShoppingCart()
    {
        List<CartLine> ShoppingCart = new List<CartLine>();
    }
}


public class ShoppingCartRepository
{

    //private List<CartLine> ShoppingCart = new List<CartLine>();


    public IEnumerable GetShoppingCart()
    {
        return ShoppingCart.ToList();
    }

    public virtual void AddItem(int productid, int quantity)
    {
        ShoppingCart.Add(new CartLine { ProductId = productid, Quantity = quantity });
    }

    public virtual void RemoveItem(int cartlineid)
    {
        ShoppingCart.RemoveAll(l => l.CartLineId == cartlineid);
    }

}

也許這會對您有所幫助。

public class CartLine
{
        public int CartLineId { get; set; }
        public int ProductId { get; set; }
        public int Quantity { get; set; }
}

    public class ShoppingCart : List<CartLine>
    {
        public ShoppingCart()
        {
        }

    }

    public class ShoppingCartRepository
    {

        private ShoppingCart ShoppingCart = new ShoppingCart();

        public IEnumerable GetShoppingCart()
        {
            return ShoppingCart.ToList();
        }

        public virtual void AddItem(int productid, int quantity)
        {
            ShoppingCart.Add(new CartLine
            {
                ProductId = productid,
                Quantity = quantity
            });
        }

        public virtual void RemoveItem(int cartlineid)
        {
            ShoppingCart.RemoveAll(l => l.CartLineId == cartlineid);
        }

    }

我更改類以使其通用。 我認為的問題是,您要使ShoppingCart成為通用列表,但要添加CartLine

希望對您有所幫助。

我不確定您為什么要將購物車移到外面,但是代碼有很多問題

  1. 您將需要在List<CartLine> ShoppingCart class ShoppingCartRepository定義List<CartLine> ShoppingCart屬性。 現在,您正在內部構造函數中創建它。 那不會使ShoppingCart成為類的屬性/字段。
  2. 如以下注釋中所述, ToList()不是List一部分。 因此,除非您明確定義它,否則將無法使用它。 但不確定為什么需要在List元素上調用ToList()
  3. 另外,您不應該繼承List<ShoppingCart> ,而應該使用interface- IList<T> 這將幫助您進行模擬/測試。

更重要的是,我沒有看到您嘗試做的任何增值。 如果您可以提供更多詳細信息,也許我可以提供更多詳細信息。

暫無
暫無

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

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