• C#简单工厂和抽象类的实例


    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();
                FileFarher ff = GetFile(fileName,path+fileName);//返回的是被之类重写的FileFarher 
                    ff.OpenFile();
                Console.ReadLine();
            }
            public static FileFarher GetFile(string filename,string fullpath)
            {
                string extension = Path.GetExtension(filename);
                FileFarher ff = null;
                switch (extension)
                {
                    case ".txt":ff = new TxTPath(fullpath);
                        break;
                    case ".jpg":ff = new JpgPath(fullpath);
                        break;
                }
                return ff;
            }
        }
        public abstract class FileFarher   //父类
        {
            public string fileName
            {
                get;
                set;
            }
            public FileFarher(string filename)
            {
                this.fileName = filename;
            }
        public abstract void OpenFile();
        }
        public class TxTPath : FileFarher  //继承父类
        {    public TxTPath(string filename) : base(filename)   //继承父类中的filename
            {
    
            }
            public override void OpenFile()
            {
                ProcessStartInfo pso = new ProcessStartInfo(this.fileName);
                Process p = new Process();
                p.StartInfo = pso;
                p.Start();
            }
        }
        public class JpgPath : FileFarher   //继承父类
        {
            public JpgPath(string filename) : base(filename) //继承父类中的filename
            {
    
            }
            public override void OpenFile()
            {
                
            }
        }
    }
    

  • 相关阅读:
    thinkphp 学习1-模型
    apache 2.4目录权限
    标头 header()函数的用法
    PHP面试题一
    php学习
    如何执行超过一百兆(100MB)的sql脚本?
    jquery-numberformatter插件
    xpath 获取父级,和同级
    Vue el-table 行编辑验证、重置
    Vue 弹窗一个新增编辑页面
  • 原文地址:https://www.cnblogs.com/CoderAyu/p/8490151.html
Copyright © 2020-2023  润新知