夜光带你走进C# 游戏开发等(八十五)擅长的领域

  • 时间:
  • 来源:互联网
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41987706/article/details/103194489

夜光序言:

 

 

不要问, 雨, 为何如此凄凉? 那天,诗人独自忧伤,没有日落的帷幕,满是风在怒狂,雷电劈落了灵魂,化成雨, 落寞流淌。

 

 

 

 

 

 

 

正文:

 

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    //依赖倒转原则  →  面向接口编程

    class Program
    {
        static void Main(string[] args)
        {
            //我们先 new 一个 diy 出来
            DIY diy = new DIY();
            Console.WriteLine("请选择CPU:");
            switch (Console.ReadLine())
            {
                case "intel":
                    diy.SelectCPU(new Intel());
                    break;
                case "amd":
                    diy.SelectCPU(new Amd());
                    break;
            }

            Console.WriteLine("请选择MEM:");
            switch (Console.ReadLine())
            {
                case "hy":
                    diy.SelectMemory(new HY());
                    break;
                case "kingston":
                    diy.SelectMemory(new Kingston());
                    break;
            }

            Console.WriteLine("您的配置清单如下:");
            diy.Print();

            Console.ReadLine();
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    public class DIY
    {
        //先定义一下
        private ICpu cpu;
        private IMemory mem;


/*        public virtual ICpu ICpu
        {
            get;
            set;
        }


        public virtual IMemory IMemory
        {
            get;
            set;
        }*/

        public virtual void SelectCPU(ICpu cpu)
        {
            this.cpu = cpu;
        }

        public virtual void SelectMemory(IMemory mem)
        {
            this.mem = mem;
        }


        public virtual void Print()
        {
            //首先,我们先输出一下CPU
            Console.WriteLine("-------------------CPU----------------------");
            cpu.ShowModel();
            cpu.ShowSpeed();

            //其次,我们输出一下MEM
            Console.WriteLine("-------------------MEM----------------------");
            mem.ShowModel();
            mem.ShowCapacity();

        }


    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    public class HY : IMemory
    {
        public virtual void ShowCapacity()
        {
            Console.WriteLine("Capacity : 1111G");
        }

        public virtual void ShowModel()
        {
            Console.WriteLine("Model : HY-1111G160802");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    class Amd : ICpu
    {
        public void ShowModel()
        {
            Console.WriteLine("Model : Amd09160802");
        }

        public void ShowSpeed()
        {
            Console.WriteLine("Speed : 3.2Ghz");
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    public interface IComponent
    {
        void ShowModel();

    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    //接口继承另一个接口
    public interface ICpu : IComponent
    {
        void ShowSpeed();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    public interface IMemory : IComponent
    {
        void ShowCapacity();

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    public class Kingston : IMemory
    {
        public void ShowCapacity()
        {
           Console.WriteLine("Capacity: 1111G");
        }

        public void ShowModel()
        {
            Console.WriteLine("Model : Kingston-1111G160802");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 案例1
{
    public class Intel : ICpu
    {
        public void ShowModel()
        {
            Console.WriteLine("Model : I709160802");
        }

        public void ShowSpeed()
        {
            Console.WriteLine("Speed : 2.4Ghz");
        }
    }

}

 

本文链接http://element-ui.cn/news/show-76.aspx