C#書き込み工場モード

10329 ワード

program.cs file
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            #region  
            DVD dvd = new DVD();
            Console.WriteLine(dvd.PlayVideo());

            VCD vcd = new VCD();
            Console.WriteLine(vcd.PlayVideo());
            #endregion
            #region  
            Test();
            #endregion

        }
        static void Test()
        {
            VideoShow vs;
            vs = new DVD();
            Play(vs);

            vs = new VCD();
            Play(vs);
        }
        static void Play(VideoShow vs)
        {
            string str = vs.PlayVideo();
            Console.WriteLine(str);
        }
    }
    public abstract class VideoShow
    {
        public abstract string PlayVideo();
    }
    public class VCD:VideoShow
    {
        public override string PlayVideo()
        {
            return " VCD";
        }
    }
    public class DVD:VideoShow
    {
        public override string PlayVideo()
        {
            return " DVD";
        }
    }
}

create.cs file
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Create
    {
        public static VideoShow factory(string VideoName)
        {
            switch (VideoName.ToUpper())
            {
                case "DVD":
                    return new DVD();
                case "VCD":
                    return new VCD();
            }
            return null;
        }
    }
    public class Test
    {
        public static void Main()
        {
            VideoShow vs = Create.factory("DVD");
            vs.PlayVideo();

            vs = Create.factory("VCD");
            vs.PlayVideo();
        }
    }
}

codefile
using System;
using System.Text;
class Program
{
    static void Main(string[] args)
    {
        DVD dvd = new DVD();
        Console.WriteLine(dvd.PlayVideo());
        VCD vcd = new VCD();
        Console.WriteLine(vcd.PlayVideo());

        TEST();
    }

    static void TEST()
    {
        VideoShow vs;
        vs = new DVD();
        Play(vs);

        vs = new VCD();
        Play(vs);
    }
    static void Play(VideoShow vs)
    {
        string str = vs.PlayVideo();
        Console.WriteLine(str);
    }
}
public abstract class VideoShow
{
    public abstract string PlayVideo();
}
public class VCD : VideoShow
{
    public override string PlayVideo()
    {
        return " VCD";
    }
}
public class DVD:VideoShow
{
    public override string PlayVideo()
    {
        return " DVD";
    }
}

インタフェースはインスタンス化できません.ただし、インタフェースオブジェクトは、その実装クラスオブジェクトを指すことができます.
C#に特徴的なものclass Example{private static Example instance;private Example(){}public static Example Instance//javaのsetやgetとは方法があまり違いませんが、形式がそんなに変で関数ではなくパラメータはありませんが、関数体{get{if(null=instance){instance=new Example();
 } return instance; }}
}