カテゴリー
C

独習C#第3版、P334 インターフェイス

using System;

namespace Chapter01All
{
    interface ChangeInt
    {
        int Routine(String str);
    }

    class InterFaceSample: ChangeInt
    {
        public int Routine(String s)
        {
            int w = 0;
            for(int i=0; i<s.Length; i++)
            {
                w = (w * 10)  + s[i] - '0';
            }
            return w;
        }
    }
    class InterfaceSample
    {
        static void Main()
        {
            InterFaceSample obj = new InterFaceSample();
            Console.WriteLine(obj.Routine("100"));
            Console.WriteLine(obj.Routine("200"));
        }
    }
}
using System;

namespace Chapter01All
{
    interface ChangeInt
    {
        int Routine(String str);
        String NotConv(String str);
    }

    class InterFaceSample: ChangeInt
    {
        public int Routine(String s)
        {
            int w = 0;
            for(int i=0; i<s.Length; i++)
            {
                w = (w * 10)  + s[i] - '0';
            }
            return w;
        }
        public String NotConv(String str)
        {
            return str;
        }
    }
    class InterfaceSample
    {
        static void Main()
        {
            InterFaceSample obj = new InterFaceSample();
            Console.WriteLine(obj.Routine("100"));
            Console.WriteLine(obj.NotConv("ABC"));
        }
    }
}

 interfaceキーワードを使うと、クラスのプロトタイプのようなもので、クラスの形態のみを定義するようです。実際にクラスを作るときは、継承のように行って、メソッドも含めて書いていくようです。文字列に数値に変換できないものがあったらどうなるのか?interface ChangeIntのメンバーが2つにはなりましたが?

コメントを残す

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

inserted by FC2 system