有谁知道任何示例或代码显示可用于像 DateTime 结构这样的距离的类或结构?我需要能够以英尺和英寸为单位添加、减去和显示数据,而使用转换方法只会变得困惑。一个类或结构将是完美的,但我在搜索中什么也没找到。

最佳答案

使用结构,但使其不可变(所有属性均为只读)。

属性应至少包括:

  • TotalFeet
  • TotalInches

  • 方法应至少包括:
  • FromFeet(静态)
  • FromInches(静态)

  • 声明私有(private)支持字段为:
    private readonly double _meters;
    

    编辑:也许是这样的。
    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);
        }
    }
    

    关于c# - 距离(英尺、英寸)类或结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2357795/

    10-17 01:19