簡體   English   中英

使用布爾方法編寫我自己的名稱問題類

[英]Writing My own class for names issue with boolean method

我正在為College寫一堂課,它所要做的就是在前一個和中間存一個名字。 它還必須有一些方法,其中一個是“public boolean equals(Name otherName)”

這是我到目前為止所擁有的

public class Name 
{
 private String FirstNM, MiddleNM, LastNM,Name, otherName;
 public Name(String first, String middle, String last)
{
    FirstNM = first;
    MiddleNM = middle;
    LastNM = last;
    Name = first+middle+last;
}

public String toString()
{
    return (FirstNM+", "+MiddleNM+", "+LastNM);
}

public String getFirst()
{
    return FirstNM;
}

public String getMiddle()
{
    return MiddleNM;
}

public String getLast()
{
    return LastNM;
}

public String firstMiddleLast()
{
    return (FirstNM+", "+MiddleNM+", "+LastNM);
}

public String lastFirstMiddle()
{
    return (LastNM+", "+FirstNM+", "+MiddleNM);
}

public boolean equals(Name otherName)
{
    if (otherName.equalsIgnoreCase(Name))
    {
        return true;
    }
}

我在將一個Name對象與另一個Name對象進行比較時遇到問題。 這個問題希望我使用equalsIgnoreCase方法。 我似乎無法讓這個工作。 我做錯了什么?我能做些什么不同的事情

編輯:讓我用書中的確切問題來澄清

  1. 寫一個類名稱,用於存儲人員的名字,中間名和姓氏,並提供以下方法:

·public Name(String first,String middle,String last)-constructor。 名稱應存放在給定的情況下; 不要轉換為所有大寫或小寫。

·public String getFirst() - 返回名字

·public String getMiddle() - 返回中間名

·public String getLast() - 返回姓氏

·public String firstMiddleLast() - 按順序返回包含人名全名的字符串,例如“Mary Jane Smith”。

·public String lastFirstMiddle() - 返回一個字符串,其中包含該人的全名,其姓氏首先后跟一個逗號,例如“Smith,Mary Jane”。

·public boolean equals(Name otherName) - 如果此名稱與otherName相同,則返回true。 比較不應區分大小寫。 (提示:有一個String方法equalsIgnoreCase就像String方法一樣,除了在進行比較時沒有考慮大小寫。)

你想在equals方法中做的是比較類中的所有重要變量。 為了幫助您入門:

public boolean equals(Name otherName) {
    return (this.firstNm.equalsIgnoreCase(otherName.firstNm) && /* rest of variables to compare */)

}

從技術上講,這應該是一個Object並投下它,但如果你的老師說要接受Name然后這樣做我猜...

覆蓋equals應該看起來更像這樣:

public boolean equals(Object other) {
    if (other == null || ! other instanceof Name) return false;
    Name otherName = (Name) other;
    return (this.firstNm.equalsIgnoreCase(otherName.firstNm) && /* rest of variables to compare */)
}

equalsIgnoreCase()用於比較不用於比較對象的字符串。 為了比較對象,您需要正確覆蓋equals方法。 equals()將根據您需要的屬性比較對象的相等性,並且hashCode()是必需的,以便在Collections正確使用對象。

這是java.lang.Object類中equals的默認實現

 public boolean equals(Object obj) {
        return (this == obj);
    }

在比較或創建getName()方法時,只需使用具有全名的String,而不是Name。 至少這樣做:

public boolean equals(Name otherName)
{
    String fullName = otherName.getFirst() + otherName.getMiddle() + otherName.getLast();
    if (fullName.get.equalsIgnoreCase(this.Name))
    {
        return true;
    }
    return false;
}

equalsIgnoreCase是必須在String對象上調用的Java String方法。 您正在嘗試在Name對象上調用此方法,這將無效。 您必須在Name類中調用幫助器方法以檢索正確的String,然后調用equalsIgnoreCase方法。

以下是一些方法,用於比較整個Name字符串和單獨的Name的3個部分。

public boolean equals(Name otherName)
{
    return (otherName.getFirst().equalsIgnoreCase(Name.getFirst()) 
            && otherName.getMiddle().equalsIgnoreCase(Name.getMiddle())
            && otherName.getLast().equalsIgnoreCase(Name.getLast()));
}

此外,您可以使用您提供的字符串方法獲得更清晰的代碼。

public boolean equals(Name otherName)
{
    return (otherName.toString().equalsIgnoreCase(Name.toString()));
}

暫無
暫無

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

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