zoukankan      html  css  js  c++  java
  • 0102-进程操作(面向对象简单工厂模式,打开输入文件)

    //本节学习用面向对象的方式,打开用户输入的文件
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace day011
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("输入文件路径");
                string fileP = Console.ReadLine();
                Console.WriteLine("输入文件名称");
                string fileN = Console.ReadLine();
                //通过简单工厂模式返回一个父类:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例
                BaseFile bf = GetFile(fileP, fileN);
                if (bf != null)
                {
                    bf.FileOpen();
                }
            }
    
            public static BaseFile GetFile(string filePath,string fileName)
            {
                BaseFile bf = null;
                string fileExt = Path.GetExtension(fileName);
                switch (fileExt)
                {
                    case ".txt":
                        bf=new TxtFile(filePath,fileName);
                        break;
                    case ".mp3":
                        bf=new Mp3File(filePath,fileName);
                        break;
                    default:
                        bf=new BaseFile(filePath,fileName);
                        break;
                }
                return bf;
            }
        }
    
        class BaseFile  //注意:正式项目中每个类要单独创建文件,不建议多个类放到一个单元文件中
        {
            //类的成员包括:字段、属性、构造函数、函数、索引器
            private string _FilePath;//字段命名使用下划线开始,创建属性快捷键:Ctrl+R+E
    
            public string FilePath
            {
                get { return _FilePath; }
                set { _FilePath = value; }
            }
    
            public string FileName { get; set; }//自动属性:使用prop+两次Tab键
    
            public BaseFile(string filePath, string fileName)
            {
                this.FilePath = filePath;
                this.FileName = fileName;
            }
            //设计一个函数用来打开指定的文件
            public void FileOpen()
            {
                ProcessStartInfo psi = new ProcessStartInfo(this.FilePath + "\" + this.FileName);
                Process pro = new Process();
                pro.StartInfo = psi;
                pro.Start();
            }
    
        }
        //设计几个子类,注意:父类的构造函数覆盖掉了无参构造函数,所以子类构造函数需显式调用
        class TxtFile : BaseFile
        {
            public TxtFile(string filePath, string fileName) : base(filePath, fileName)
            {
            }
        }
        class Mp3File : BaseFile
        {
            public Mp3File(string filePath, string fileName) : base(filePath, fileName)
            {
            }
        }
    }
    凡哥,别他妈吹牛逼了
  • 相关阅读:
    C# 图片与Base64的相互转化
    LeetCode 303. Range Sum Query – Immutable
    LeetCode 300. Longest Increasing Subsequence
    LeetCode 292. Nim Game
    LeetCode 283. Move Zeroes
    LeetCode 279. Perfect Squares
    LeetCode 268. Missing Number
    LeetCode 264. Ugly Number II
    LeetCode 258. Add Digits
    LeetCode 257. Binary Tree Paths
  • 原文地址:https://www.cnblogs.com/sdlz/p/14686242.html
Copyright © 2011-2022 走看看