博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单工厂设计模式-模拟磁盘打开文件
阅读量:6915 次
发布时间:2019-06-27

本文共 2895 字,大约阅读时间需要 9 分钟。

using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _模拟磁盘打开文件{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("请选择要进入的磁盘");            string path = Console.ReadLine();            Console.WriteLine("请选择要打开的文件");            string fileName = Console.ReadLine();            //文件的全路径:path+fileName            FileFather ff = GetFile(fileName,path+fileName);            ff.OpenFile();            Console.ReadKey();        }        public static FileFather GetFile(string fileName,string fullPath)//通过方法返回父类类型        {            string extension  = Path.GetExtension(fileName);//获取扩展名            FileFather ff = null;            switch(extension)            {                case ".txt": ff = new TxtPath(fullPath);//传参A                    break;                case ".jpg": ff = new JpgPath(fullPath);                    break;                case ".wmv": ff = new WmvPath(fullPath);                    break;            }            return ff;        }        public abstract class FileFather        {            public string fullPath//需要全路径 来打开,做成自动属性,存储全路径            {                get;                set;            }            //文件的全路径:fileName(fulPath)=path+fileName,合成 做一个构造函数//传参A            public FileFather(string fullPath)//传参A            {                this.fullPath = fullPath;            }            //方法1 传参,方法2 属性            public abstract void OpenFile();        }        public class TxtPath : FileFather        {            public TxtPath(string fullPath)                : base(fullPath)            {            }            public override void OpenFile()            {                //子类继承父类的属性,参数传入属性fileName                ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);                Process  p = new Process();                p.StartInfo = psi;                p.Start();                        }        }        public class JpgPath  : FileFather        {            public JpgPath(string fullPath)                : base(fullPath)//构造函数,调用父类            {            }            public override void OpenFile()            {                ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);                Process p = new Process();                p.StartInfo = psi;                p.Start();               }        }        public class WmvPath : FileFather        {            public WmvPath(string fullPath)                : base(fullPath)            {            }            public override void OpenFile()            {                ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);                Process p = new Process();                p.StartInfo = psi;                p.Start();            }        }    }}

 

转载于:https://www.cnblogs.com/blacop/p/5985997.html

你可能感兴趣的文章
构建之法阅读笔记二。
查看>>
梦断代码阅读笔记一。
查看>>
【python】-- 多进程的基本语法 、进程间数据交互与共享、进程锁和进程池的使用...
查看>>
linux虚拟机使用VMware的NAT共享windows主机IP上网 [转]
查看>>
Rabbitmq编程
查看>>
C++虚函数
查看>>
Android记住密码后自动登录
查看>>
python 訪问webservice
查看>>
CSDN开源夏令营 百度数据可视化实践 ECharts(4)
查看>>
SVN 初试
查看>>
安装edX DevStack
查看>>
避开Unity的坑
查看>>
微软Windows Phone今日正式面向中国市场发布
查看>>
bzoj1112 [POI2008]砖块Klo
查看>>
235D Graph Game
查看>>
csu 1984: LXX的能力值
查看>>
汉编随想(一)
查看>>
开源的Android开发框架-------PowerFramework使用心得(五)网络请求HTTPRequest
查看>>
[转载]kmeans
查看>>
一个不错的架构图:基于SpringCloud的微服务项目
查看>>