Jump to content
Rtrix

Java Help

Recommended Posts

Posted
Write a method equals for the Module class that tests if two Module objects have the

same module name. It should start like this

public boolean equals(Module module)

O mana de ajutor?

Posted (edited)

public boolean equals(Module module)

{

if (module != null)

{

if (this.name == module.GetName())

{

return true;

}else

{

return false;

}

}

return false;

}

// sau mai scurt

public 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 by M2G
Posted (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 by Ganav

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...