有没有人知道任何示例或代码显示可以像DateTime结构一样用于距离的类或结构?我需要能够以英尺和英寸为单位添加,减去和显示数据,并且使用转换方法会变得混乱.一个类或结构将是完美的,但我在搜索中没有发现任何东西.
Sam*_*ell 12
使用结构,但使其不可变(所有属性都是get-only).
属性应至少包括:
方法应至少包括:
将私有支持字段声明为:
private readonly double _meters;
Run Code Online (Sandbox Code Playgroud)
public struct Distance : IEquatable<Distance>, IComparable<Distance>
{
private static readonly double MetersPerKilometer = 1000.0;
private static readonly double CentimetersPerMeter = 100.0;
private static readonly double CentimetersPerInch = 2.54;
private static readonly double InchesPerFoot = 12.0;
private static readonly double FeetPerYard = 3.0;
private static readonly double FeetPerMeter = CentimetersPerMeter / (CentimetersPerInch * InchesPerFoot);
private static readonly double InchesPerMeter = CentimetersPerMeter / CentimetersPerInch;
private readonly double _meters;
public Distance(double meters)
{
this._meters = meters;
}
public double TotalKilometers
{
get
{
return _meters / MetersPerKilometer;
}
}
public double TotalMeters
{
get
{
return _meters;
}
}
public double TotalCentimeters
{
get
{
return _meters * CentimetersPerMeter;
}
}
public double TotalYards
{
get
{
return _meters * FeetPerMeter / FeetPerYard;
}
}
public double TotalFeet
{
get
{
return _meters * FeetPerMeter;
}
}
public double TotalInches
{
get
{
return _meters * InchesPerMeter;
}
}
public static Distance FromKilometers(double value)
{
return new Distance(value * MetersPerKilometer);
}
public static Distance FromMeters(double value)
{
return new Distance(value);
}
public static Distance FromCentimeters(double value)
{
return new Distance(value / CentimetersPerMeter);
}
public static Distance FromYards(double value)
{
return new Distance(value * FeetPerYard / FeetPerMeter);
}
public static Distance FromFeet(double value)
{
return new Distance(value / FeetPerMeter);
}
public static Distance FromInches(double value)
{
return new Distance(value / InchesPerMeter);
}
public static Distance operator +(Distance a, Distance b)
{
return new Distance(a._meters + b._meters);
}
public static Distance operator -(Distance a, Distance b)
{
return new Distance(a._meters - b._meters);
}
public static Distance operator -(Distance a)
{
return new Distance(-a._meters);
}
public override bool Equals(object obj)
{
if (!(obj is Distance))
return false;
return Equals((Distance)obj);
}
public bool Equals(Distance other)
{
return this._meters == other._meters;
}
public int CompareTo(Distance other)
{
return this._meters.CompareTo(other._meters);
}
public override int GetHashCode()
{
return _meters.GetHashCode();
}
public override string ToString()
{
return string.Format("{0}[m]", TotalMeters);
}
}
Run Code Online (Sandbox Code Playgroud)
我之前编写过自包含的单元转换器类,但我不知道.NET是一个很好的公共类.
话虽如此,它写起来相当容易 - 只需制作一个可以从英寸或英尺构造的结构,并可转换为两者.
public struct Distance
{
private Distance(int inches)
{
this.totalInches = inches;
}
private int totalInches;
public int Inches { get { return this.totalInches % 12; } }
public int Feet { get { return this.totalInches / 12; } }
public static Distance FromInches(int inches)
{
return new Distance(inches);
}
public static Distance FromFeet(int feet)
{
return new Distance(feet * 12);
}
public static Distance FromFeetAndInches(int feet, int inches)
{
return new Distance(feet * 12 + inches);
}
}
Run Code Online (Sandbox Code Playgroud)