简体   繁体   English

从方法设置只读属性

[英]Setting read-only property from a method

I have a simple problem, but I'm stuck as a newbie. 我有一个简单的问题,但是我还是新手。

My SetGrade method takes a float parameter and I would like it to return a char and then set that value to the Grade property. 我的SetGrade方法接受一个float参数,我希望它返回一个char,然后将该值设置为Grade属性。

I'm not doing something correctly. 我没有正确地做某事。

public Class Student {
    private char grade;

    public char Grade { get { return grade; } }

    public char SetGrade(float score) {
        char Mgrade;
        if(score >= 90.0) {
            return Mgrade = 'A';
        }
        return Mgrade = 'F';
     }
}

There are numerous problems with this code but they might not be what you think they are. 这段代码有很多问题,但可能不是您认为的那样。

First off, Public is wrong; 首先, Public是错误的; C# requires public . C#需要public

Second, the use of the local Mgrade is strange and unnecessary, but interestingly enough not actually wrong ; 其次,使用本地Mgrade是奇怪且不必要的,但有趣的是,实际上并没有 it is legal to do an assignment and a return in one step like that. 像这样一步地完成任务和回报是合法的。 But in this case you do not need to; 但是在这种情况下,您无需这样做; just return 'A'; 只需return 'A'; without the local assignment. 没有本地分配。

Third, the method is misnamed because it does not set the Grade property . 第三,该方法名称错误,因为它没有设置Grade属性 If you intend it to set the grade then it should be void returning: 如果您打算用它来设置等级,那么返回它应该是无效的:

public void SetGrade(float score)
{
   if(score >= 90.0)
   {
      this.grade = 'A';
   }
   this.grade = 'F';
} 

If instead the method is intended to be a conversion from floats to chars then it should be static: 相反,如果该方法旨在从floats转换为chars,则它应该是静态的:

public static char ScoreToGrade(float score)
{
   if(score >= 90.0)
   {
      return 'A';
   }
   return 'F';
} 

Frankly, I'd be inclined to do both: 坦白说,我倾向于两者都做:

public void SetGrade(float score)
{
  this.grade = ScoreToGrade(score);
} 

There, now you've got the best of both worlds. 在那里,您已经两全其美。

Fourth, this is just a stylistic point; 第四,这只是一个风格问题。 you might consider: 您可能会考虑:

  public char Grade { get; private set; }

the compiler will generate an "invisible" backing field for you, so you don't have to manage it yourself. 编译器将为您生成一个“不可见”的后备字段,因此您不必自己管理它。 This syntax means that Grade can be read from anywhere and written to from within this class. 这种语法意味着可以从任何地方读取Grade并可以在该类中写入Grade

No need to assign your character to an intermediate char variable. 无需将您的字符分配给中间char变量。 Just return correct character like this. 只是这样返回正确的字符。

public char SetGrade(float score)
{
   if(score >= 90.0)
   {
      return 'A';
   }
   return 'F';
}

Your syntax is a bit off: 您的语法有些偏离:

  public char SetGrade(float score)
  {
     if(score >= 90.0)
     {
       return 'A';
     }
     return 'F';
  }

No need for the Mgrade variable and you should simply return the character wanted, instead of an assignment and return. 不需要Mgrade变量,您应该简单地返回所需的字符,而不是赋值并返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM