java - Equals Method for assertEquals with String-objects to compare -


i want override equals method can compare 2 variable-objects if equal object has string "abc" variable, , object has string "abc" variable. need class in order manage , parse expressions.

so want make testclass testing wether variable-objects equal. want override that.

@override public boolean equals(object obj) {     return this.variable.equals(((variable) obj).getvariable()); } 

now problem not quite sure if work later, since use equals-method here, used before compare strings. since override it, doesn't mean use same method now? or variable-strings using equals-method comparing strings? if thats not working, else should compare string in overwritten equals method?

as @jbnizet said, method, of right now, throw classcastexception if obj not of right type. should work on first:

@override public boolean equals(object obj) {     if (!obj instanceof variable) {         return false;     }     return this.variable.equals(((variable) obj).getvariable()); } 

that being said, newly defined equals applies variable objects, not strings. that's why call equals() within method still work, if that's question, because references string.equals(), because variable field string (i guessed), not variable.

also, equals should overriden way if mean consider variable objects equal when variable field same.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -