关于MyEclipse中的java小程序
为什么控制台不能输出菜单?程序如下:
package restaurant;
import java.util.*;
class Meun {
int number;
String name;
double money;
public Meun(int num,String n,double m){
number=num;
name=n;
money=m;
}
}
public class R {
public static void load(){
Set<Meun> m=new HashSet<Meun>();
m.add(new Meun(1,"西冷牛排",108));
m.add(new Meun(2,"红酒猪扒",82));
m.add(new Meun(3,"黑椒意大利面",46));
m.add(new Meun(4,"木瓜西米露",38));
m.add(new Meun(5,"菲力牛排",120));
m.add(new Meun(6,"蘑菇汤",32));
Iterator<Meun> list=m.iterator();
while(list.hasNext());
{
System.out.println(list.next());
}
}
public static void main(String[] args){
load();
}
}
[解决办法]
System.out.println的参数实际上是调用对象的toString方法,而你的类Meun 并没有重写toString方法,那么也就是调用Object类的toString方法,所以控制台输出的就是类名后面跟一串内存地址
你将下面的toString方法添加到Menu类里面就可以看到效果了
public String toString(){StringBuffer sb = new StingBuffer();sb.append("num:");sb.append(num);sb.append("\t name:");sb.append(name);sb.append("\t money:");sb.append(money);return sb.toString();}