[英]Overloading >= operator
I need to overload operator >=
. 我需要重载运算符
>=
。 If the condition is true, the operator returns true
, otherwise false
. 如果条件为true,则运算符返回
true
,否则返回false
。 If at least one of the objects is null
– throw an exception ( ArgumentException
). 如果至少一个对象为
null
,则引发异常( ArgumentException
)。 I tried this. 我试过了 What's wrong?
怎么了?
public static bool operator false(Staff inputPerson)
{
if ((inputPerson.salary) <= 15000)
{
return true;
}
else if ((inputPerson.salary) is null)
{
throw new ArgumentOutOfRangeException("This person does not have a job");
}
return false;
}
You need to do something like public static bool operator <= (Rational rational1, Rational rational2)
您需要执行类似
public static bool operator <= (Rational rational1, Rational rational2)
When you overload this, you need to ensure that you overload all the related operators too. 重载时,还需要确保所有相关的运算符也重载。 eg <, > <=, >= etc. as well as the equality operators and methods.
例如<,> <=,> =等,以及相等运算符和方法。
You need to pass in both of the objects to be compared, as the method is static, and not an instance method. 您需要传入两个要比较的对象,因为该方法是静态的,而不是实例方法。
Try this: 尝试这个:
public static bool operator >=(Staff p1, Staff p2) {
if (p1 is null || p2 is null) {
throw new ArgumentOutOfRangeException("This person does not have a job");
}
return p1.salary >= p2.salary;
}
Source: http://msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx 来源: http : //msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.