.Net学习笔记----2015-07-21(C#基础复习03,简单工厂和抽象类)
2015-07-21 10:02
1121 查看
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; }
代码冗余,主要为练习抽象类和抽象方法
相关文章推荐
- C#创建Windows Service(Windows 服务)基础教程
- VS2015, .NET 4.6, C# 6.0, F# 4.0等重量级产品正式上线
- C#设计模式 - 观察者模式(使用委托)
- VS2015, .NET 4.6, C# 6.0, F# 4.0等重量级产品正式上线
- C#如何打开并读取usb的文件目录
- C#中循环语句:while、for、foreach的使用
- C# NetCut / NetworkCut
- 微软今日发布汇总:VS2015, .NET 4.6, C# 6.0, F# 4.0等重量级产品正式上线
- C# 事件
- 微软今日发布汇总:VS2015, .NET 4.6, C# 6.0, F# 4.0等重量级产品正式上线
- C#线程入门(一)
- C#6.0中10大新特性的应用和总结
- 十三、C# 事件
- C#委托类型 - (System.Delegate)
- C# array
- c#中xmL文件生成操作的4中类型说明
- C# 无法识别的转义序列
- c# 强制杀掉进程
- C#高级编程----小结
- C#静态构造函数