カテゴリー
C

独習C#第3版、P482 型パラメータの制約、基本クラス制約

using System;

namespace Chapter01All
{
    class MyStrMethods2
    {
        public string ReverseStr(string str)
        {
            string w = "";
            for( int i=0; i<str.Length; i++)
            {
                w = str[i] + w;
            }
            return w;
        }
    }
    class Test<T> where T : MyStrMethods2
    {
        T obj;
        public Test( T o)
        {
            obj = o;
        }
        public void ShowReverse(string str)
        {
            string w = obj.ReverseStr(str);
            Console.WriteLine(w);
        }
    }
    class Demo
    {
        static void Main()
        {
            MyStrMethods2 objA = new MyStrMethods2();
            Test<MyStrMethods2> t1 = new Test<MyStrMethods2>(objA);
            t1.ShowReverse("This is a test.");
        }
    }
}

基本クラス制約は、型パラメータに当てはめる型(つまり、型引数)が、特定の型から継承しなけらばならないという制限を加えるものです。上の例ではclass Testはclass MyStrMethods2を継承していることを制限するということですかね。それをwhere T : MyStrMethods2と書く、ということですか?

using System;

namespace Chapter01All
{
    class MyStrMethods2
    {
        public string ReverseStr(string str)
        {
            string w = "";
            for( int i=0; i<str.Length; i++)
            {
                w = str[i] + w;
            }
            return w;
        }
    }
    class Test<T> where T : MyStrMethods2
    {
        T obj;
        public Test( T o)
        {
            obj = o;
        }
        public void ShowReverse(string str)
        {
            string w = obj.ReverseStr(str);
            Console.WriteLine(w);
        }
    }
    class MyClass : MyStrMethods2 { }
    class Demo
    {
        static void Main()
        {
            MyStrMethods2 objA = new MyStrMethods2();
            Test<MyStrMethods2> t1 = new Test<MyStrMethods2>(objA);
            t1.ShowReverse("This is a test.");

            MyClass objB = new MyClass();
            Test<MyClass> t2 = new Test<MyClass>(objB);
            t2.ShowReverse("This is a test,too.");
        }
    }
}

class Demoの後半部分が追加になってます。継承の仕方が最初の例と後半で少し違います。

using System;

namespace Chapter01All
{
    class MyStrMethods3
    {
        string str;
        public MyStrMethods3(string Str = "default string")
        {
            str = Str;
        }
        
        public void ShowReverse()
        {
            Console.WriteLine(ReverseStr());
        }
        public string ReverseStr()
        {
            string w = "";
            for (int i = 0; i < str.Length; i++)
            {
                w = str[i] + w;
            }
            return w;
        }
    }
 
    class MyClass : MyStrMethods3 { }
    class Demo
    {
        static void Main()
        {
            MyStrMethods3 objA = new MyStrMethods3("This is a test.");
            objA.ShowReverse();
           

            MyClass objB = new MyClass();
            objB.ShowReverse();
            
        }
    }
}

 面倒なことを止めて、基本だったら、こうでしょう。ReverseStrのアクセス修飾子がpublicになってますが、privateで問題ないと思います。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

inserted by FC2 system