public static void Main(string[] args)
        {
            Display b1 = new StringDisplay("Hello, world.");

            // b1にSiderBorderの能力を付加する。
            Display b2 = new SideBorder(b1, '#');

            // b2にFullBorderの能力を付加する。
            Display b3 = new FullBorder(b2);

            b1.Show();
            b2.Show();
            b3.Show();

            // Border(の派生クラス)は、Displayの派生クラスだから、SideBorder, FullBorderには、
            // SiderBorder, FullBorderのオブジェクトを渡せる。
            Display b4 =
                new SideBorder(
                    new FullBorder(
                        new FullBorder(
                            new SideBorder(
                                new FullBorder(
                                    new StringDisplay("こんにちは。")
                                    ),
                                '*'
                                )
                            )
                        ),
                    '/'
                    );

            b4.Show();
            Console.ReadLine();
        }
Beispiel #2
0
        public static void Main(System.String[] args)
        {
            // 'H'を持ったCharDisplayのインスタンスを1個作る。
            AbstractDisplay d1 = new CharDisplay('H');

            // "Hello, world."を持ったStringDisplayのインスタンスを1個作る。
            AbstractDisplay d2 = new StringDisplay("Hello, world.");

            // "こんにちは。"を持ったStringDisplayのインスタンスを1個作る。
            AbstractDisplay d3 = new StringDisplay("こんにちは。");

            d1.Display(); // d1,d2,d3とも、すべて同じAbstractDisplayのサブクラスのインスタンス
            d2.Display(); // だから、継承したdisplayメソッドを呼び出すことができる。
            d3.Display(); // 実際の動作は個々のクラスCharDisplayやStringDisplayで定まる。
        }