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();
                }
            }
        }
    }
  • 相关阅读:
    Servlet(简介,请求参数,页面跳转,生命周期,创建,配置,ServletContext,线程)
    zookeeper 集群配置
    centos yum 安装 mariadb
    Oracle jdbc 插入 clob blob
    oracle 删除用户,提示“无法删除当前已连接的用户”
    oracle 创建表空间,用户并授权
    Linux 根分区扩容
    同时运行多个 tomcat 修改端口
    mysql新建用户及授权
    php sql 类似 mybatis 传参
  • 原文地址:https://www.cnblogs.com/blacop/p/5985997.html
Copyright © 2011-2022 走看看