作者TonyQ (骨头)
看板java
标题Re: [问题] JAVA的call by value
时间Sat Mar 24 22:53:00 2007
※ 引述《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