当前位置: 代码迷 >> J2SE >> 菜鸟请问大侠们一个小程序
  详细解决方案

菜鸟请问大侠们一个小程序

热度:46   发布时间:2016-04-24 01:08:43.0
初学者请教大侠们一个小程序。
package package1;

import java.util.*;

public class TestStudent {
public static void main(String args[]) {
Manage m = new Manage();
m.add();
}
}

class Students {
String name;
String number;
String score;

Students(String name, String number, String score) {
this.name = name;
this.number = number;
this.score = score;

}
}

class Manage {
public static LinkedList l = new LinkedList();

public void add() {
Students ysj = new Students("zs", "20111130123", "100");
Students wqk = new Students("ls", "20111130121", "99");
Students lxd = new Students("ww", "20111130111", "0");
l.add(ysj);
l.add(wqk);
l.add(lxd);
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}

}
}



为什么上面那段代码打印的结果是

package1.Students@1bc4459
package1.Students@12b6651
package1.Students@4a5ab2


我想得到下面的结果请问该如何修改

zs, 20111130123, 100
ls, 20111130121, 99
ww, 20111130111, 0


求高人指教 谢谢了!

------解决方案--------------------
给Students 加一方法,覆写继承的 toString().

代码:
Java code
public String toString(){  retrun name+", "+number+", "+score;}
------解决方案--------------------
package package1;

import java.util.*;

public class TestStudent {
public static void main(String args[]) {
Manage m = new Manage();
m.add();
}
}

class Students {
String name;
String number;
String score;

Students(String name, String number, String score) {
this.name = name;
this.number = number;
this.score = score;

}
public String toString(){
return name+","+number+","+score;
}

}

class Manage {
public static LinkedList l = new LinkedList();


public void add() {
Students ysj = new Students("zs", "20111130123", "100");
Students wqk = new Students("ls", "20111130121", "99");
Students lxd = new Students("ww", "20111130111", "0");
l.add(ysj);
l.add(wqk);
l.add(lxd);
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}
}

}


  相关解决方案