简体   繁体   中英

how will i make the equal method in a class and how will i call that method to the main if that class was an abstract class

We were given a problem about subscribing and i was able to run it already but my only problem was i wasn't able to call my equal method for the reason of not knowing how. The problem says that if a user will enter an address the other user must not enter the same address and the equal method will verify if that address is already existing.

here is my equal method .. (is my equal method correct?)

public boolean equals(NewsPaperSubscriber address){
    return getStreetAdd() == address.streetAdd;
}

}

A little more of your code would help, but from what I see:

There are a few things to note on the code you posted.

Firstly, I'm assuming that the getter getStreetAdd() returns the String variable streetAdd . In that case, then why not use the getter for the object address as well? Not using the getter means your address field is public , make it private instead then use:

address.getStreetAdd()

Secondly, == compares the object references themselves, not the value of whatever the object contains. Example:

String string1 = "abc"
String string2 = "abc"
return (string1 == string2) // will return FALSE, because string1 and string2
                            // point to different locations in memory

Therefore, you can use the .equals() method provided by the String class. Combining both these things, your code should now look like:

public boolean equals(NewsPaperSubscriber address){
    return getStreetAdd().equals(address.getStreetAdd);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM