Re: [問題] JAVA的call by value
※ 引述《ldg (ldg)》之銘言:
: 抱歉小弟還有一個問題沒有搞懂
: 先謝過各位這麼熱心指點
: class student{
: int num;
: static void A(){
: ...
: }
: public static void main(String[] args){
: student[] s=new student[3];
: int i;
: for(i=0;i<3;i++)
: s[i]=new student();
: ...
: }
: }
: 請問有沒有方法
: 可以讓A這個method
: 在不改變其參數(傳入值)的條件下
: 讓這個method可以處理s[]
: 有點像是在
: public static student[] s=new student[3];
: (當然這是錯的)
: 那有方法可以達到這種概念嗎?
: 抱歉問題有點多
: 但是希望起頭時就接觸正確的資訊
: 以養成OO的正確習慣
: 謝謝指點了
static當然可以用。(扣除掉語意問題,它沒有絕對的錯誤。)
不過站在oo的角度來看,與其處理s[],
應該讓student自己處理自己。
就像底下的作法這樣,如果你給予每個student不同的num值,
可以展示的更清楚點。
: class student{
: int num;
: void printmyself(){
: System.out.println("hi, I'am "+num);
: }
: public static void main(String[] args){
: student[] s=new student[3];
: int i;
: for(i=0;i<3;i++)
: s[i]=new student();
: ...
for(i=0;i<3;i++) s[i].printmyself();
: }
: }
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.27.68
討論串 (同標題文章)
完整討論串 (本文為第 7 之 7 篇):