看板 java 關於我們 聯絡資訊
最近在學JAVA 看到書上一個object method 後面再接 method 的用法 讓我不懂為什麼可以這樣使用 //=================建立一個GUI按鈕======================== import javax.swing.* public class SimpleGuil{ public static void main(String[] args){ JFrame frame = new JFrame(); JButton button = new JButton("click me"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(button); //就是上面這句為什麼可以是一個 method 再接一個 method?? //難道 method 裡面還可以再定義一個 method?? frame.setSize(300,300); frame.setVisible(true); } } //===========程式結束============================ 難道那 method 是這樣定義的?? public class JFrame{ public getContentPane(){ public add(){ //程式處理 } } } 感覺又好像不能這樣子定義 我自己試著這種方式定義 method //=========測試程式================== public class Test{ public int a(){ public int b(){ return 1; } } public static void main(String[] args){ Test m = new Test(); int i = m.a().b(); System.out.println(i); } } //============END===================== 卻無法執行 編譯器一直判斷 method 中的 method 有問題 這到底是為什麼?? 要怎麼樣才能做出書中那種 object method 後面還能接 method ??? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 115.43.211.230
a761007:如果你m.a()回傳的是Test object 你就能m.a().b() 08/24 21:42
yellowbooky:我想應該是frame.getContentPane()已經回傳一個容器 08/24 21:50
yellowbooky:物件了 再用物件的add方法可行 08/24 21:53
lvlightvivi:應該要a()的回傳物件含有b()函式才能用 08/24 21:58