Rtrix Posted October 14, 2014 Report Posted October 14, 2014 Write a method equals for the Module class that tests if two Module objects have thesame module name. It should start like thispublic boolean equals(Module module)O mana de ajutor? Quote
M2G Posted October 14, 2014 Report Posted October 14, 2014 (edited) public boolean equals(Module module){ if (module != null) { if (this.name == module.GetName()) { return true; }else { return false; } } return false;}// sau mai scurtpublic boolean equals(Module module){ if (module != null) { return (this.name == module.GetName()) ? true : false; } return false;}// sau public boolean equals(Module module){ if (module != null) return this.name == module.GetName(); return false;}Presupunand ca ai un membru de clasa "name" care reprezinta numele modulului si este private. Atunci mai ai si o metoda publica de get pentru acea variabila (GetName).this.name == module.GetName() mai poti sa-l scrii si this.name.equals(module.GetName()) @Ganav ai dreptate. Nu am mai scris de mult java. Edited October 14, 2014 by M2G Quote
Ganav Posted October 14, 2014 Report Posted October 14, 2014 (edited) Presupunand Module este o clasa apriori definita si nu tine de cuvantul cheie module poti folosi reflection:public boolean equals(Module module) { if(this.getClass().getName().equals(module.getClass().getName())) return true; return false;}Metoda de mai sus trebuie inclusa, fireste, in corpul clasei.Edit: @M2G atentie == verifica pentru egalitatea intre referinte(adica adrese in memorie) nu continut. .equals() verifica daca doua string-uri au intr-adevar aceleasi caractere in aceleasi pozitii. Edited October 14, 2014 by Ganav Quote