请教一下,原本写读取纪录变量的txt档,类似这样
System.IO.StreamReader file = new System.IO.StreamReader("file.txt");
ClassFather xxx = new ClassFather();
string str = file.ReadLine();
xxx.strA = str;
str = file.ReadLine();
xxx.strB = str;
后来改写,想由文字档的第一行判断要用父或子类别,之后的处理都一样的
System.IO.StreamReader file = new System.IO.StreamReader("file.txt");
string str = file.ReadLine();
if (str == "Father")
{
ClassFather xxx = new ClassFather();
str = file.ReadLine();
xxx.strA = str;
str = file.ReadLine();
xxx.strB = str;
}
else if (str == "Son")
{
ClassSon xxx = new ClassSon();
str = file.ReadLine();
xxx.strA = str;
str = file.ReadLine();
xxx.strB = str;
}
因为后面的xxx.strA和xxx.strB程式码都长一样,
有没有只要写一次的方法呢,新手不知道该搜寻什么关键字,
还请多见谅