看板 C_Sharp 關於我們 聯絡資訊
最近在學物件導向的觀念 剛好看到大話設計模式這本書附錄有提供相關內容 所以就依此研究 書中提到一個範例 首先自訂一個button(貓叫) 當按下button時觸發click事件跳出提示框(喵) 程式碼: private void button_Click(object sender, EventArges e) { MessageBox.Show(Shout()); } class Cat { public string Shout() { return "喵"; } } 但是我在visual studio中輸入時 跳出「名稱Shout不存在目前內容中」 我想問題應該是出在class cat的位置 請問版上高手class cat 應該如何擺放 才能讓button使用class Cat內的Shout() 謝謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.177.119.114 ※ 編輯: FrankYoshi 來自: 180.177.119.114 (05/27 20:47)
totte:要先new出一隻貓 Cat cat = new Cat(); 再呼叫cat.Shout(); 05/27 21:44
FrankYoshi:原來如此~OK了~謝謝你 05/27 21:59
tomoyaken14:MessageBox.Show(new Cat().Shout()); 05/27 22:00
KanoLoa:改 public static string Shout(),可以直接Cat.Shout() 05/28 01:09