前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#简单工厂和抽象类的实例

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

作者头像
码农阿宇
发布2018-04-18 10:43:16
6000
发布2018-04-18 10:43:16
举报
文章被收录于专栏:码农阿宇码农阿宇
代码语言:javascript
复制
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()
        {
            
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-12-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档