A45 Polymorphisme


polymorphisme c++ c# f# vbPour utiliser un même nom de méthode quelque soit la dérivation il faut la déclarer virtuelle dans la classe base et la redéfinir dans la classe idoine
<- héritage C#       BASES       classe abstraite C# ->

Polymorphisme en C#     C++    F#    VB

using System;

namespace polymorphismeCs
{
    class Program
    {
        static void Main(string[] args)
        {
            var homme = new Personne( "Dugenou" , "Marc" );
            var femme = new PersonneF( "Dugenou" , "Michelle" , "Durand" );
            affichage(homme); // appel fonction
            affichage(femme);

            Console.ReadKey();
        }
        // Fonction qui accepte le polymorphisme ( Personne ou PersonneF )
        public static void affichage(Personne p)
        {
            p.affiche();
        }

        // Classe base
        // ¨¨¨¨¨¨¨¨¨¨¨
        public class Personne
        {
            // attributs d'instance
            string nom, prénom;
            //constructeur
            public Personne(string nom, string prénom)
            {
                Nom = nom;
                Prénom = prénom;
            }
            //accesseurs
            public string Nom {
                get { return nom; }
                set
                {
                    if (value == null || value.Trim().Length == 0)
                        nom = "?" ;
                    else nom = value;
                }
            }
            public string Prénom
            {
                get { return prénom; }
                set
                {
                    if (value == null || value.Trim().Length == 0)
                        prénom = "?" ;
                    else prénom = value;
                }
            }
            //affichage
            public virtual void affiche() // déclaré virtuel
            {
                Console.WriteLine(nom + " " + prénom);
            }
        }

        // Classe dérivée
        // ¨¨¨¨¨¨¨¨¨¨¨¨¨¨
        class PersonneF : Personne
        {
            //attribut d'instance
            string nomJeuneFille;
            //constructeur
            public PersonneF(string nom, string prénom, string nomJeuneFille)
                : base(nom, prénom)
            {
                NomJeuneFille = nomJeuneFille;
            }
            //accesseurs
            public string NomJeuneFille
            {
                get { return nomJeuneFille; }
                set
                {
                    if (value == null || value.Trim().Length == 0)
                        nomJeuneFille = "?" ;
                    else nomJeuneFille = value;
                }
            }
            //affichage
            public override void affiche() // redéfinition (override)
            {
                Console.WriteLine(base.Nom + " ( " + nomJeuneFille + " ) " +
                    base.Prénom);
            }
        }
    }
}

Affichage dans la console C#

Dugenou Marc
Dugenou ( Durand ) Michelle

<- héritage C#                                        BASES                                   classe abstraite C# ->

Laisser un commentaire