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(); } } }}