1. 程式人生 > 遊戲 >《碧藍幻想Versus》季票第二彈第五名角色揭曉:尤斯提斯將參戰!

《碧藍幻想Versus》季票第二彈第五名角色揭曉:尤斯提斯將參戰!

使用場景:

1 使用時不用知道具體呼叫的類。

2 類初始化較複雜

使用情況如下:

SchoolFactory schoolFactory = new SchoolFactory();
School a = schoolFactory.CreateSchool(SchoolTypeEnum.普通中學);
a.Dosomething();
 a = schoolFactory.CreateSchool(SchoolTypeEnum.職業中學);
a.Dosomething();
a = schoolFactory.CreateSchool(SchoolTypeEnum.軍校);
a.Dosomething();
a = schoolFactory.CreateSchool(SchoolTypeEnum.工廠);
a.Dosomething();
a = schoolFactory.CreateSchool(SchoolTypeEnum.飯店);
a.Dosomething();

實現程式碼如下:

    class SchoolFactory
    {
        public School CreateSchool(SchoolTypeEnum typename)
        {
            string schoolName = "建華";
            int age = 2;
            switch (typename)
            {
                case SchoolTypeEnum.普通中學:
                    return new OrdinaryMiddleSchool(schoolName + typename, age + 1);
                case SchoolTypeEnum.職業中學:
                    return new VocationalHighSchool(schoolName + typename, age + 2);
                case SchoolTypeEnum.軍校:
                    return new MilitaryAcademy(schoolName + typename, age + 3);
                case SchoolTypeEnum.工廠:
                default:
                    return new Factory(schoolName + typename, age + 4);
            }
        }
    }
abstract class School
    {
        protected string _name;
        protected int _age;
        public School(string name, int age)
        {
            _name = name;
            _age = age;
        }

        public void Dosomething()
        {
            Introduce();
            Teching();
            Console.WriteLine("");
        }

        protected void Introduce()
        {
            Console.WriteLine(_name + " 建立" + _age + "年:");
        }

        public virtual void Teching()
        {
        }
    }

    class OrdinaryMiddleSchool : School
    {
        public OrdinaryMiddleSchool(string name, int age)
            : base(name, age)
        {

        }

        public override void Teching()
        {
            Console.WriteLine("教數學,語文,英語");
        }
    }
    class VocationalHighSchool : School
    {
        public VocationalHighSchool(string name, int age)
            : base(name, age)
        {

        }

        public override void Teching()
        {
            Console.WriteLine("教電焊,汽車維修,電腦操作,做菜");
        }
    }
    class MilitaryAcademy : School
    {
        public MilitaryAcademy(string name, int age)
            : base(name, age)
        {

        }

        public override void Teching()
        {
            Console.WriteLine( " 教武裝越野,武器操作");
        }
    }
    class Factory : School
    {
        public Factory(string name, int age)
            : base(name, age)
        {

        }

        public override void Teching()
        {
            Console.WriteLine(" 教擰螺絲,掃地,端菜");
        }
    }
    enum SchoolTypeEnum
    {
        普通中學,
        職業中學,
        軍校,
        工廠,
        飯店
    }