For faster navigation, this Iframe is preloading the Wikiwand page for Sobrecàrrega de mètodes.

Sobrecàrrega de mètodes

La Sobrecàrrega de mètodes (en anglès function overloading) és quan es defineixen dos mètodes amb el mateix nom i a la mateixa classe però amb diferents implementacions. L'execució d'una crida a un mètode sobrecarregat dependrà aleshores del context de la crida.

Per exemple, ferTasca() i ferTasca(Objecte O) són mètodes sobrecarregats. Per cridar al primer no cal que li passem cap paràmetre, en canvi el segon necessita un paràmetre de tipus Objecte. Aleshores segons la crida que s'hagi fet s'executarà una implementació o una altra.

Cada llenguatge de programació té les seves peculiaritats per fer ús de la sobrecàrrega de mètodes,[1] per exemple en Java es pot tenir la funció o mètode sobrecarregat amb el mateix nombre de paràmetres però essent de tipus diferent,[2] ja que si fossin iguals, el compilador no sabria quin triar.[3] I en C++ es poden donar valors per defecte als paràmetres,[4] de manera que quan es crida a la funció, si s'omet el paràmetre, aquest obtindrà el valor per defecte assignat en la seva declaració.

Regles per la sobrecàrrega

[modifica]

Bàsicament podem sobrecarregar mètodes a partir de dues regles generals, tot i que podem trobar altres regles segons el llenguatge que estiguem utilitzant.

  • Tenir diferent nombre d'arguments
  • Si hi ha el mateix nombre d'arguments que siguin de tipus diferents
  • Que el mètode o funció retorni un tipus de dada o un altre és indiferent

Exemples

[modifica]
public int suma(int a, int b)
public long suma(long a, long b)
public long suma(long a, long b, long c)

Sobrecàrrega de constructors

[modifica]

La sobrecàrrega de constructors s'utilitza per crear o instanciar objectes de formes diferents. Per exemple tenim aquests tres constructors d'un objecte anomenat Persona en llenguatge java on podem crear una Persona a partir del seu nom i els dos cognoms, a partir del seu nom i només el seu primer cognom o bé la Persona sense cap dada.

public class Persona {
 private String Nom, Cognom1, Cognom2;

 //Constructor Persona amb Nom i 2 cognoms
 public Persona(String n, String c1, String c2) {
 Nom = n;
 Cognom1 = c1;
 Cognom2 = c2;
 }

 //Constructor Persona amb Nom i el primer cognom
 public Persona(String n, String c1) {
 Nom = n;
 Cognom1 = c1;
 Cognom2 = null;
 }

 //Constructor Persona sense cap dada
 public Persona() {
 Nom = null;
 Cognom1 = null;
 Cognom2 = null;
 }

}

Referències

[modifica]

Enllaços externs

[modifica]
{{bottomLinkPreText}} {{bottomLinkText}}
Sobrecàrrega de mètodes
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?