請教一下,原本寫讀取紀錄變數的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程式碼都長一樣,
有沒有只要寫一次的方法呢,新手不知道該搜尋什麼關鍵字,
還請多見諒