简体   繁体   English

如何制作高效的getter和setter方法

[英]How to make efficient getters and setters method

I am trying to get largest from three integers of 5 objects, the code works, but looks kind of bulky and inefficient, is there any way I can improve it?我试图从 5 个对象的三个整数中获得最大,代码有效,但看起来有点笨重且效率低下,有什么办法可以改进它吗? can I also combine one method getLargest to compute largest, smallest and average, without using an array?我还可以结合一种方法getLargest来计算最大、最小和平均值,而不使用数组吗?

  class Calc
  {
    private int num1, num2, num3, Ans;

    public int ans  { get { return Ans; }  set { Ans = value; } }
    public int Num1 { get { return num1; } set { num1 = value; } }
    public int Num2 { get { return num2; } set { num2 = value; } }
    public int Num3 { get { return num3; } set { num3 = value; } }

    public void getLargest(int x, int y, int z)
    {
         ans=(Math.Max(Math.Max(x, y), z));
    } 
  }

Technically you can make this a lot smaller...从技术上讲,您可以将其缩小很多......

public class Calc
{
    public int Max => Math.Max(Math.Max(Num1, Num2), Num3);
    public int Min => Math.Min(Math.Min(Num1, Num2), Num3);
    public int Avg => (Num1 + Num2 + Num3) / 3;
    public int Num1 { get; set; }
    public int Num2 { get; set; }
    public int Num3 { get; set; }
}

(This requires using System.Linq; at the top) (这需要using System.Linq;在顶部)

What this does is creates auto-properties (properties with no bodies) which automatically creates the backing fields for you.这样做是创建自动属性(没有主体的属性),它会自动为您创建支持字段。

Technically this is a little more inefficient as it will calculate them every time, so what you would want to do is something more like this:从技术上讲,这有点低效,因为它每次都会计算它们,所以你想要做的是更像这样的事情:

public class Calc
{
    public int Num1 { get; private set; }
    public int Num2 { get; private set; }
    public int Num3 { get; private set; }
    public int Max { get; private set; }
    public int Min { get; private set; }
    public int Avg { get; private set; }

    public Calc(int num1, int num2, int num3)
    {
        Num1 = num1;
        Num2 = num2;
        Num3 = num3;
        Max = Math.Max(Math.Max(Num1, Num2), Num3);
        Min = Math.Min(Math.Min(Num1, Num2), Num3);
        Avg = (Num1 + Num2 + Num3) / 3;
    }
}

(There are different ways to construct this object, the above is just an easy-to-understand method of doing so). (这个对象的构造方法有很多种,以上只是简单易懂的方法)。 This will "cache" the results in an immutable object (which is something you want to learn about).这会将结果“缓存”在一个不可变对象中(这是您想了解的内容)。

As you've specified without using an array, I suppose this is your best option for "cleaner", though I doubt this produces any noticeable speed improvements, is there a reason you don't want to use an array?正如您在不使用数组的情况下指定的那样,我认为这是“更清洁”的最佳选择,尽管我怀疑这会产生任何明显的速度改进,但您是否有不想使用数组的原因? What's the purpose of the class?上课的目的是什么?

class Calc
{

    public int ans { get; set; }

    public int Num1 { get; set; }

    public int Num2 { get; set; }

    public int Num3 { get; set; }

    public void getLargest(int x, int y, int z)
    {
        ans = Math.Max(Math.Max(x, y), z);
    }

}

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

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