.Net學習筆記----2015-07-21(C#基礎複習03,簡單工廠和抽象類別)

來源:互聯網
上載者:User

標籤:



static void Main(string[] args) { //使用進程開啟指定檔案 ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\Administrator\Desktop\Adobe註冊機使用說明.txt"); Process p = new Process(); p.StartInfo = psi; p.Start(); }

會和前面的筆記重複,但是還是複習一遍吧,上次就沒太整明白

類比控制台開啟檔案:(複習物件導向)

1、在控制台提示使用者要進入的磁碟路徑

D:\

2、提示使用者輸入要開啟的檔案的名稱

1.txt

這樣就獲得了D:\1.txt    這是檔案的全路徑

 

父類:

    public abstract class FileFather    {        public string FileName        {            get;            set;        }        public FileFather(string fileName)        {            this.FileName = fileName;        }        public abstract void OpenFile();    }}

TxtPath子類:

    class TxtPath : FileFather    {        public TxtPath(string fileName)            : base(fileName)        {        }        public override void OpenFile()        {            ProcessStartInfo psi = new ProcessStartInfo(this.FileName);            Process p = new Process();            p.StartInfo = psi;            p.Start();        }    }

JpgPath子類:

    class JpgPath : FileFather    {        public JpgPath(string fileName)            : base(fileName)        {        }        public override void OpenFile()        {            ProcessStartInfo psi = new ProcessStartInfo(this.FileName);            Process p = new Process();            p.StartInfo = psi;            p.Start();        }    }

WmvPath子類:

    class WmvPath : FileFather    {        public WmvPath(string fileName)            : base(fileName)        {        }        public override void OpenFile()        {            ProcessStartInfo psi = new ProcessStartInfo(this.FileName);            Process p = new Process();            p.StartInfo = psi;            p.Start();        }    }

程式入口:

        static void Main(string[] args)        {            //使用進程開啟指定檔案            //ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\Administrator\Desktop\Adobe註冊機使用說明.txt");            //Process p = new Process();            //p.StartInfo = psi;            //p.Start();            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);                    break;                case ".jpg": ff = new JpgPath(fullPath);                    break;                case ".wmv": ff = new WmvPath(fullPath);                    break;            }            return ff;        }

代碼冗餘,主要為練習抽象類別和抽象方法

.Net學習筆記----2015-07-21(C#基礎複習03,簡單工廠和抽象類別)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.