-4

我已经突出显示了下面给我这个错误的代码。我想要做的是,如果 的值nserved小于或等于 4,则返回该值,但是如果大于 4,则默认值为 0。

PS 我刚开始编程的时间不长,我试图让我的代码尽可能简单。谢谢您的帮助。

public class FoodItem
{
    private String Name;
    private String Description;
    private int NumberServed;
    private double Cost;

    public FoodItem(String fname, String fdescription, int nserved, double fcost)
    {
        Name = fname;
        Description = fdescription;
        NumberServed = nserved;
        Cost = fcost;
    }

     public string getFoodName()
     {
         return Name;
     }

     public void setFoodName(string fname)
     {
         Name = fname;
     }

     public string getFoodDescription()
     {
         return Description;
     }

     public void setFoodDescription(string fdescription)
     {
         Description = fdescription;
     }

     public int getNumberPeople()
     {
         return NumberServed;
     }

     public void setNumberPeople(int nserved)
     {
         NumberServed = nserved;

         // Checks to see if the maximum number of people has been exceeded.
         if (nserved <= 4)
         {
             return nserved;
         }
         else
         {
             return 0;
         }
     }

     public double getFoodCost()
     {
         return Cost;
     }

     public void setFoodCost(double fcost)
     {
         Cost = fcost;
     }
}
4

3 回答 3

1

考虑改为使用带有 getter/setter 的普通属性:

public class FoodItem
{
    public const in MaxNumberOfPeople = 4;
    // no extra logic for this field
    public string Name {get;set;} 

    // this one have backing field and some special logic for set:
    private int numberServed;
    public int NumberServed {
       get { return numberServed;}
       set 
       {
           /* some special code here if needed*/
           numberServed = value > MaxNumberOfPeople ? 0 : value;
       } 
    }
}
于 2013-11-11T20:31:53.757 回答
0

首先,您有一个奇怪的方法,它声明它什么也不返回 (is void),将一个私有字段设置为传递的数字,然后尝试返回一个受约束的值。您所追求的可能是以下内容:

 public void setNumberPeople(int nserved)
 {
     if (nserved <= 4)
     {
         NumberServed =  nserved;
     }
     else
     {
         NumberServed = 0;
     }
 }

虽然这是冗长的代码,但可以更简洁地表示为:

 public void setNumberPeople(int nserved)
 {
     NumberServed = (nserved <= 4) ? nserved : 0;
 }

最后,整个班级看起来就像您了解 Java 并且正在为 C# 编译器编写 Java。C# 支持一流的属性,整个事情可以重写为:

public class FoodItem
{
    private int numberServed;

    public FoodItem(String fname, String fdescription, int nserved, double fcost)
    {
        FoodName = fname;
        FoodDescription = fdescription;
        NumberPeople = nserved;
        FoodCost = fcost;
    }

    public string FoodName { get; set; }
    public string FoodDescription { get; set; }
    public int NumberPeople() { get; set; }
    public double FoodCost { get; set; }

    public int NumberPeople
    {
        get { return numberServed; }
        set { numberServed = value <= 4 ? value : 0; }
    }
}
于 2013-11-11T20:36:45.117 回答
0

您的方法签名public void setNumberPeople(int nserved)意味着它基本上没有返回任何内容,或者至少没有返回可用数据。

如果要返回整数,请将签名更改为:

public int setNumberPeople(int nserved).

于 2013-11-11T20:05:51.147 回答