Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Utilisation de ‘as’ avec Interface*

Exemple,d'un,attribut,obsolète:,non,edité
{filelink=18818}

using System;
 
public interface IPrintMessage
{
    void Print();
};
 
class Class1
{
    public void Print()
    {
        Console.WriteLine("Hello from Class1!");
    }
}
 
class Class2 : IPrintMessage
{
    public void Print()
    {
        Console.WriteLine("Hello from Class2!");
    }
}
 
class MainClass
{
    public static void Main()
    {
        PrintClass   PrintObject = new PrintClass();
 
        PrintObject.PrintMessages();
    }
}
 
class PrintClass
{
    public void PrintMessages()
    {
        Class1      Object1 = new Class1();
        Class2      Object2 = new Class2();
 
        PrintMessageFromObject(Object1);
        PrintMessageFromObject(Object2);
    }
 
    private void PrintMessageFromObject(object obj)
    {
        IPrintMessage PrintMessage;
 
        PrintMessage = obj as IPrintMessage;
        if(PrintMessage != null)
            PrintMessage.Print();
    }
}

No comments yet.

Leave a Comment