簡體   English   中英

重載> =運算符

[英]Overloading >= operator

我需要重載運算符>= 如果條件為true,則運算符返回true ,否則返回false 如果至少一個對象為null ,則引發異常( ArgumentException )。 我試過了 怎么了?

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;
}

您需要執行類似public static bool operator <= (Rational rational1, Rational rational2)

重載時,還需要確保所有相關的運算符也重載。 例如<,> <=,> =等,以及相等運算符和方法。

您需要傳入兩個要比較的對象,因為該方法是靜態的,而不是實例方法。

嘗試這個:

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;
}

來源: http : //msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM