2009年12月23日水曜日

.演算子を使用し、Interface間での同じメソッドの衝突を回避してみる。

using System;


interface Interface1
{
    void printOut(string s);
}
interface Interface2
{
    void printOut(string s);
}
class TestClass : Interface1, Interface2
{
    void Interface1.printOut(string s)
    {
        Console.WriteLine("Interface1 " + s);
    }
    void Interface2.printOut(string s)
    {
        Console.WriteLine("Interface2 " + s);
    }
}


class MainClass
{
    static void Main()
    {
        TestClass test = new TestClass();
        // TestClass型をInterface1型にキャスト
        Interface1 i1 = (Interface1)test;
        // 出力値:Interface1 Interface
        i1.printOut("Interface1");
        // TestClass型をInterface2型にキャスト
        Interface2 i2 = (Interface2)test;
        // 出力値 Interface2 Interface
        i2.printOut("Interface2");
    }
}

0 コメント:

コメントを投稿