我可以从运算符重载工作中进行这种隐式转换吗?
Can I make this implicit conversion from an operator overload work?
我正在尝试将 class 中的除法运算符重载为 return 双精度数。
我有两个 class:Length
和 Angle
。在 Angle
class 中,我有接受不同三角比的初始值设定项。这是一个例子:
public class Angle
{
public double Degrees;
public double Minutes;
public double Etc;
public Angle(double radians)
{
// Main initialization here.
}
public static Angle FromTangent(double tangent)
{
return new Angle(Math.Atan(tangent));
}
}
Length
class 将测量输入转换为不同的测量单位。最后一种方法真的会让生活变得轻松:
public class Length
{
public double Inches;
public double Feet;
public double Meters;
public double Etc;
public enum Unit { Inch, Foot, Meter, Etc };
public Length(double value, Unit unit)
{
// Main initialization here.
}
public static Length operator /(Length dividend, Length divisor)
{
double meterQuotient = dividend.Meters / divisor.Meters;
return new Length(meterQuotient, Unit.Meter);
}
// This is what I want to be able to do.
public static double operator /(Length dividend, Length divisor)
{
double ratio = dividend.Meters / divisor.Meters;
return ratio;
}
}
问题是最后两个方法有歧义。我做了一些研究,隐式转换似乎是正确的学习路径。我尝试了以下方法,但似乎语法不正确:
public static implicit operator double /(Length dividend, Length divisor) { }
public static double implicit operator /(Length dividend, Length divisor) { }
public static implicit double operator /(Length dividend, Length divisor) { }
最终
我希望能够分割两个 Length
对象,并得到一个双倍的对象。不过它只对除法有效,因为它 return 是一个比率,而不是单位数。如果这是可能的,那么实施将非常简单,而且很棒。这就是为什么我想知道这是否可能。
Length opposite = new Length(userInputValue, userSelectedUnitOfMeasure);
Length adjacent = new Length(otherInputValue, otherUnitOfMeasure);
Angle angle = Angle.FromTangent(opposite / adjacent); // ← So cool if this is possible
能否在保持我的其他除法运算符过载的同时完成此操作?
转换不是除法 - 它们是两个独立的操作。您目前似乎正在尝试将它们结合起来。
从根本上说,您似乎应该删除此运算符:
// Kill this
public static Length operator /(Length dividend, Length divisor)
这根本就没有意义 - 正如您所说,长度除以长度是一个比率,而不是长度。 5m / 2m是2.5,不是2.5m。
一旦删除,就没有歧义了,所以你很好。
另一方面,对我来说,拥有英寸、英尺、米等字段似乎不是个好主意。您可能需要两个字段,其中一个是幅度,另一个是单位(可能是一个枚举)。
我正在尝试将 class 中的除法运算符重载为 return 双精度数。
我有两个 class:Length
和 Angle
。在 Angle
class 中,我有接受不同三角比的初始值设定项。这是一个例子:
public class Angle
{
public double Degrees;
public double Minutes;
public double Etc;
public Angle(double radians)
{
// Main initialization here.
}
public static Angle FromTangent(double tangent)
{
return new Angle(Math.Atan(tangent));
}
}
Length
class 将测量输入转换为不同的测量单位。最后一种方法真的会让生活变得轻松:
public class Length
{
public double Inches;
public double Feet;
public double Meters;
public double Etc;
public enum Unit { Inch, Foot, Meter, Etc };
public Length(double value, Unit unit)
{
// Main initialization here.
}
public static Length operator /(Length dividend, Length divisor)
{
double meterQuotient = dividend.Meters / divisor.Meters;
return new Length(meterQuotient, Unit.Meter);
}
// This is what I want to be able to do.
public static double operator /(Length dividend, Length divisor)
{
double ratio = dividend.Meters / divisor.Meters;
return ratio;
}
}
问题是最后两个方法有歧义。我做了一些研究,隐式转换似乎是正确的学习路径。我尝试了以下方法,但似乎语法不正确:
public static implicit operator double /(Length dividend, Length divisor) { }
public static double implicit operator /(Length dividend, Length divisor) { }
public static implicit double operator /(Length dividend, Length divisor) { }
最终
我希望能够分割两个 Length
对象,并得到一个双倍的对象。不过它只对除法有效,因为它 return 是一个比率,而不是单位数。如果这是可能的,那么实施将非常简单,而且很棒。这就是为什么我想知道这是否可能。
Length opposite = new Length(userInputValue, userSelectedUnitOfMeasure);
Length adjacent = new Length(otherInputValue, otherUnitOfMeasure);
Angle angle = Angle.FromTangent(opposite / adjacent); // ← So cool if this is possible
能否在保持我的其他除法运算符过载的同时完成此操作?
转换不是除法 - 它们是两个独立的操作。您目前似乎正在尝试将它们结合起来。
从根本上说,您似乎应该删除此运算符:
// Kill this
public static Length operator /(Length dividend, Length divisor)
这根本就没有意义 - 正如您所说,长度除以长度是一个比率,而不是长度。 5m / 2m是2.5,不是2.5m。
一旦删除,就没有歧义了,所以你很好。
另一方面,对我来说,拥有英寸、英尺、米等字段似乎不是个好主意。您可能需要两个字段,其中一个是幅度,另一个是单位(可能是一个枚举)。