|
Exemple 10-6 extrait du chapitre Le système de types
Exemple 10-5< > Exemple 10-7
Cet exemple peut être compilé avec la ligne de commande: csc.exe /target:exe Exemple_10_6.cs Erreurs: 0 Avertissements: 0 Remarque:
Exemple_10_6.cs
class Article {
public string Description;
public int Prix;
}
class Commande : System.ICloneable {
public int Quantite;
public Article Article;
public override string ToString() {
return "Commande: " + Quantite + " x " + Article.Description +
" Coût total: " + Article.Prix * Quantite;
}
public object Clone() {
// Copie superficielle
return this.MemberwiseClone();
}
}
class Program {
static void Main() {
Commande commande = new Commande();
commande.Quantite = 2;
commande.Article = new Article();
commande.Article.Description = "Chaussure";
commande.Article.Prix = 80;
System.Console.WriteLine(commande);
Commande commandeClone = commande.Clone() as Commande;
commandeClone.Article.Description = "Veste";
System.Console.WriteLine(commande);
}
}
Copyright Patrick Smacchia 2006 2007
|