我收到错误“返回void,返回关键字后面不能跟一个对象表达式”

时间:2013-11-11 20:02:20

标签: c#

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

P.S。我不久就开始编程了,我试图尽可能简化代码。谢谢你的帮助。

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

3 个答案:

答案 0 :(得分: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;
       } 
    }
}

答案 1 :(得分:0)

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

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

public int setNumberPeople(int nserved)

答案 2 :(得分:0)

首先,你有一个奇怪的方法声明它不返回任何内容(是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; }
    }
}