[問題] 請問一個關於method的問題
最近在學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
→
08/24 21:42, , 1F
08/24 21:42, 1F
推
08/24 21:50, , 2F
08/24 21:50, 2F
推
08/24 21:53, , 3F
08/24 21:53, 3F
推
08/24 21:58, , 4F
08/24 21:58, 4F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):