|
Exemple 12-13 extrait du chapitre
Héritage/dérivation polymorphisme et abstraction
Exemple 12-12< > Exemple 12-14
Cet exemple peut être compilé avec la ligne de commande: csc.exe /target:exe Exemple_12_13.cs Erreurs: 0 Avertissements: 0 Remarque:
Exemple_12_13.cs
interface I {
void SetState(int i);
int GetState();
}
struct Struct : I {
private int i;
public void SetState(int i) { this.i = i; }
public int GetState() { return i; }
}
class Program {
static void Main() {
Struct s = new Struct();
// Ici un boxing de la structure est réalisé implicitement.
I i = (I)s;
s.SetState(10);
i.SetState(20);
System.Console.WriteLine("Retour de s.GetState():"+ s.GetState());
System.Console.WriteLine("Retour de i.GetState():"+ i.GetState());
}
}
Copyright Patrick Smacchia 2006 2007
|