1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
class Personne
{
// Propriétés, avec ou sans accesseur : une fois l'objet instancié, leurs valeurs sont conservées, tant qu'il y a un référence à l'instance.
public string Nom;
public string Prenom { get; set; }
private string Civilite;
public string NomComplet
{
get
{
// Variable locale : dès qu'on sort du bloc entre accolade, elle est détruite et inaccessible.
string tmp = string.Format("{0} {1} {2}", this.Civilite, this.Prenom, this.Nom);
return tmp;
}
}
// Constructeur : accessible qu'au moment de la création d'une instance. N'est plus appelable une fois l'instance créée.
public Personne(string civilite, string nom, string prenom)
{
this.Civilite = civilite;
this.Nom = nom;
this.Pernom = prenom;
}
public void ChangerNom(string nouveaunom)
{
// Variable détruite à la sortie de la méthode
string old = this.Nom;
if (old != nouveaunom)
{
this.Nom = nouveaunom;
}
}
} |
Partager