zoukankan      html  css  js  c++  java
  • 简单工厂设计模式-模拟磁盘打开文件

     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();
                }
            }
        }
    }
  • 相关阅读:
    数组对象---数据存储
    运行vue项目时,无法自动打开页面怎么办?
    数组扁平化
    数组去重
    CSS-----------text-transform
    CSS3-----width:max-content,min-content和fit_content属性
    可迭代对象
    bit和byte的 区别?
    前端常见跨域问题?
    HackerRank "Lucky Numbers"
  • 原文地址:https://www.cnblogs.com/blacop/p/5985997.html
Copyright © 2011-2022 走看看