您的位置:首页 > 编程语言 > C#

.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;
}


代码冗余,主要为练习抽象类和抽象方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: