[問題] windows form程式加入自訂class

看板C_Sharp (C#)作者 (作別人生命中的天使!)時間12年前 (2013/05/27 20:46), 編輯推噓1(103)
留言4則, 4人參與, 最新討論串1/1
最近在學物件導向的觀念 剛好看到大話設計模式這本書附錄有提供相關內容 所以就依此研究 書中提到一個範例 首先自訂一個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)

05/27 21:44, , 1F
要先new出一隻貓 Cat cat = new Cat(); 再呼叫cat.Shout();
05/27 21:44, 1F

05/27 21:59, , 2F
原來如此~OK了~謝謝你
05/27 21:59, 2F

05/27 22:00, , 3F
MessageBox.Show(new Cat().Shout());
05/27 22:00, 3F

05/28 01:09, , 4F
改 public static string Shout(),可以直接Cat.Shout()
05/28 01:09, 4F
文章代碼(AID): #1HerMld1 (C_Sharp)
文章代碼(AID): #1HerMld1 (C_Sharp)