创建支持不同单位(摄氏度、华氏度、开尔文)的温度 class

Creating a Temperature class which supports different units (Celsius, Fahrenheit, Kelvin)

我正在研究 class 以帮助我在单位之间进行转换,我需要询问创建此作品的最佳方法。

这是我关于温度的示例代码:

        public class Temperature
{
    private double _Celsius = 0;
    public double Celsius
    {
        get { return _Celsius; }
        set
        {
            _Fahrenheit = (value * 9 / 5) + 32;
            _Kelvin = value + 273.15;
        }
    }
    private double _Fahrenheit = 0;
    public double Fahrenheit
    {
        get { return _Fahrenheit; }
        set
        {
            _Celsius = (value - 32) * 5/9;
            _Kelvin = _Celsius + 273.15;
        }
    }
    private double _Kelvin = 0;
    public double Kelvin
    {
        get { return _Kelvin; }
        set
        {
            _Celsius = value - 273.15;
            _Fahrenheit = (_Celsius * 9 / 5) + 32;
        }
    }
}

我希望知道是否还有其他好的class?

我猜您可能不想加载温度对象,您只想将一个数字转换为另一个数字。所以我会考虑将 Temperature 设为静态 class 即

public static Temperature

所以你可以写一个类似

的方法
public static double GetKelvinFromCelsius(double celsius) {
     return (celsius + 273.15);
}

可以这样使用:

double kelvinTemperature = Temperature.GetKelvinFromCelsius(400);

为什么你有 三个 个字段 _Celsius_Fahrenheit_Kelvin 而只有一个(比如 _Celsius)够了:

  public class Temperature {
    private double _Celsius

    public double Celsius {
      get {
        return _Celsius;
      }
      set {
        // Simplest: no validation (e.g. -500 degree is an evident error)
        _Celsius = value;
      } 
    }

    public double Kelvin {
      get { 
        return Celsius + 273.15;
      }  
      set {
        Celsius = value - 273.15;
      } 
    }

    public double Fahrenheit {
      get { 
        return Celsius * 9 / 5 + 32;
      }  
      set {
        Celsius = (value - 32) * 5 / 9;
      } 
    }
  } 

现在让我们考虑一下使用例程的预期方式是什么?例如。我想将 100 Fahrenheit 转换为 Kelvin:

  double result = new Temperature() { Fahrenheit = 100.0 }.Kelvin;

有点罗嗦和违反直觉,对吧?可能更好的选择是实施一堆 static methods?并且根本不创建任何 class?

  public static class Temperatures {
    public static double FahrenheitToCelsius(double value) {...} 

    public static double CelsiusToKelvin(double value) {...}

    public static double FahrenheitToKelvin(double value) {
      return CelsiusToKelvin(FahrenheitToCelsius(value)); 
    }
    ...
  } 

  ...

  double result = Temperatures.FahrenheitToKelvin(100.0); 

温度基本上是一种 "complex number." 它可以用不同的测量单位进行测量,但最终您仍然应该能够比较两个不同的温度以确定它们是否相等,或者一个是 "greater than" 另一个。您可能还想将两个温度相加或相减。

我看到这个 class 的行为类似于 .NET 提供的 TimeSpan class。您的 Temperature class' 实现将选择一个单独的测量单位 - 比方说 Kelvin。温度然后提供 "convenience" 属性,这些属性以您想要的任何测量单位(华氏度或摄氏度)提供温度:

public class Temperature
{
    private double _kelvin = 0.0;
    public double Kelvin
    { 
        get { return _kelvin; }
        set { _kelvin = value; }
    }

    public double Fahrenheit 
    {
        get { return /* convert Kelvin to Fahrenheit */; }
        set { _kelvin = /* convert Fahrenheit to Kelvin */; }
    }


    public double Celsius
    {
        get { return /* convert Kelvin to Celsius */; }
        set { _kelvin = /* convert Celsius to Kelvin */; }
    }
}

另外 - 就像 TimeSpan class - 你需要静态方法,它允许你使用你当时喜欢的任何 "base measurement" 单位快速轻松地创建你的温度的实例:

/* Also inside the Temperature class: */
public static Temperature FromKelvin(double kelvin)
{
    var temperature = new Temperature();
    temperature.Kelvin = kelvin;
    return temperature;
}

public static Temperature FromFahrenheit(double fahrenheit)
{
    var temperature = new Temperature();
    temperature.Fahrenheit = fahrenheit;
    return temperature;
}

public static Temperature FromCelsius(double celsius)
{
    var temperature = new Temperature();
    temperature.Celsius = celsius;
    return temperature;
}

使用这种方法,您会得到一些简洁的使用语法;

var myTemperatureInKelvin = Temperature.FromCelsius(100.0).Kelvin;

最后 - 为了得到一些 真正的 使用这样的 class - 你会想要 implement comparison and arithmetic operators 平等,大于和小于 -而不是比较、加法、减法等。这使您可以非常清晰地比较多个温度或将多个温度加在一起;

var resultInFahrenheit = (Temperature.FromCelsius(100.0) + Temperature.FromKelvin(200.0)).Fahrenheit;

疯了!有用!