[問題] 有關virtual & override

看板C_Sharp作者 (我要快樂起來)時間16年前 (2010/03/28 23:30), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串1/2 (看更多)
請問如果子類別繼承了父類別,但是父類別無法修改成virtual 要如何才能使用override改寫父類別的成員呢? 下面程式碼是父類別的成員,要如何override才能 set 這個filename? public string FileName { get { HttpPostedFile postedFile = this.PostedFile; string str = string.Empty; if (postedFile == null) { return str; } string fileName = postedFile.FileName; try { return Path.GetFileName(fileName); } catch { return fileName; } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.37.93.238

03/29 01:21, , 1F
this.PostedFile 是 private嗎? 囧.....
03/29 01:21, 1F

03/29 15:56, , 2F
試著用new這個關鍵詞吧,但記得不要用父介面指向子物件
03/29 15:56, 2F

03/29 15:56, , 3F
可以參考5046篇相關的應用說明
03/29 15:56, 3F

03/31 21:06, , 4F
謝謝P大~還是有疑問,我在您之前發表的那篇文章有提書問題
03/31 21:06, 4F
文章代碼(AID): #1BhtOTag (C_Sharp)
文章代碼(AID): #1BhtOTag (C_Sharp)