【问题标题】:Shopping Basket list method needs a return type c#购物篮列表方法需要返回类型 c#
【发布时间】:2013-11-09 22:55:24
【问题描述】:

您好,我正在尝试为一些课程制作一个购物篮程序,我已经获得了一种不同类型的“购物篮”,但我无法让我的列表继续更新。所以我遇到的问题是'ShoppingBasketList()'需要一个返回类型,但在我得到的例子中它没有。我花了很长时间试图找出原因,但我就是不能。如果有人有任何想法将是一个很大的帮助!

public class ShoppingBasket
{
    public List<ShoppingBasketItem> Items { get; private set; }        


     public ShoppingBasketList()
    {
      Items = new List<ShoppingBasketItem>();
    }

    internal static void AddToList(string productName, int quantity, decimal latestPrice)
    {
        for (int i = 0; i < Items.Count; i++)
        {
            // if the item is already in the list
            if (Items[i].ItemName == productName)
            {
                Items[i].UpdateShoppingBasketList(quantity, latestPrice);
                return;
            }
        }

        // It's not in the list
        ShoppingBasketItem sbi = new ShoppingBasketItem(productName, quantity, latestPrice);
        Items.Add(sbi);
    }
}

【问题讨论】:

  • public ShoppingBasketList() { ... } 是一个构造函数声明。将名称更改为ShoppingBasket 或将您的班级名称更改为ShoppingBasketList
  • public class ShoppingBasket 需要构造函数public ShoppingBasketShoppingBasketList 在这里不是有效的构造函数名称,在这种情况下,它将被视为需要 void 或类型作为返回类型的方法。

标签: c# methods return shopping


【解决方案1】:
public ShoppingBasketList()
{
    Items = new List<ShoppingBasketItem>();
}

是构造函数声明(因为它没有指定返回类型)。构造函数应始终与其所属的类同名。您的构造函数称为ShoppingBasketList,而您的类称为ShoppingBasket。您应该将您的类重命名为ShoppingBasketList 将您的构造函数重命名为ShoppingBasket

例如。

public ShoppingBasket()
{
    Items = new List<ShoppingBasketItem>();
}

您可以阅读更多关于构造函数的信息here

【讨论】:

  • 是的,有人在给你之前对示例进行了调整并重命名了类并且忘记重命名构造函数。
  • 谢谢!不敢相信我花了这么长时间才一个字:P
【解决方案2】:

除了上面那些正确声明您应该将 ShoppingBasketList() 重命名为 ShoppingBasket() 的人之外,您不需要具有返回值的方法来查看您的商品。您已经将 Items 声明为 List。从您的 ShoppingBasket() 对象中,您将通过 myShoppingBasket.Items 获取您的列表

【讨论】:

    【解决方案3】:

    您的程序正在通知您返回类型,因为您指定了return;。因此删除for 中的return

    for (int i = 0; i < Items.Count; i++)
    {
        // if the item is already in the list
        if (Items[i].ItemName == productName)
        {
            Items[i].UpdateShoppingBasketList(quantity, latestPrice);
            return; //replace me with something else...
        }
    }
    

    改为添加looping invariantnotFound(或break)。

    Boolean notFound = true;
    for (int i = 0; i < Items.Count && notFound; i++)
    {
        // if the item is already in the list
        if (Items[i].ItemName == productName)
        {
            Items[i].UpdateShoppingBasketList(quantity, latestPrice);
            notFound = false; //exiting the the loop
        }
    }
    

    哦,你的方法 ShoppingBasketList 被写成构造函数(任何 public &lt;Insert Name Here&gt; 都是构造函数),在声明类时会调用它,例如 ShoppingBasket sb = new ShoppingBasket();

     public ShoppingBasketList()
    {
      Items = new List<ShoppingBasketItem>();
    }
    

    要么将它重命名为你的类名public ShoppingBasket,要么完全删除它并在declaration 中初始化你的Items

    【讨论】:

      猜你喜欢
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      相关资源
      最近更新 更多