0

可能重复:
Java 对象默认 toString

为什么当我从文件阅读器打印我的列表时它是 [myServiceOrder@3bc1cac, myServiceOrder@32fe621e, myServiceOrder@5adbb9b9, myServiceOrder@f7e4f49, myServiceOrder@2d874991, myServiceOrder@ceee5f1, myServiceOrder@183a37d9]

public class myServiceOrder implements ServiceOrder, Comparable<myServiceOrder>{

private int number=0;
private String ownerName="";
private String make="";
private String model="";
private int year=0;

public myServiceOrder(int number, String ownerName, String make, String model,  int year) {
    this.number=number;
    this.ownerName=ownerName;
    this.make=make;
    this.model=model;
    this.year=year;
}

public myServiceOrder() {
    // TODO Auto-generated constructor stub
}

@Override
public void setOrderNum(int orderNumber) {
    number=orderNumber;
}

@Override
public void setYear(int year) {
    this.year=year;
}

@Override
public void setOwner(String ownerName) {
    this.ownerName=ownerName;
}

@Override
public void setMake(String make) {
    this.make=make;
}

@Override
public void setModel(String model) {
    this.model=model;

}

@Override
public String getOwner() {
    return ownerName;
}

@Override
public String getMake() {
    return make;
}

@Override
public String getModel() {
    // TODO Auto-generated method stub
    return  model;
}

@Override
public int getOrderNum() {
    // TODO Auto-generated method stub
    return number;
}

@Override
public int getYear() {
    // TODO Auto-generated method stub
    return year;
}

@Override
public String getMakeModelYear() {
    // TODO Auto-generated method stub
    return make+ " "+ model+ " "+ year+ " ";
}

@Override
public boolean equals(ServiceOrder otherServiceOrder) {
        if (getOrderNum()==otherServiceOrder.getOrderNum())
            return true;
        else
    return false;
}

@Override
public int compareTo(ServiceOrder otherServiceOrder, int key) {
    int comparisonResult=0;
    if(key==1)
    {
        if(getOrderNum()< otherServiceOrder.getOrderNum())
            comparisonResult= -1;
        if(getOrderNum()== otherServiceOrder.getOrderNum())
            comparisonResult= 0;
        if(getOrderNum()> otherServiceOrder.getOrderNum())
            comparisonResult= 1;
    }
        else if(key==2)
        {
            comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());                      
        }

        else if(key==3)
        {
         comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());
        }
    return comparisonResult;

}

@Override
public int compareTo(myServiceOrder arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import javax.swing.JOptionPane;


public class List extends LinkedList<myServiceOrder> {
private static  LinkedList<myServiceOrder> newList = new LinkedList();

 public  void  Print() throws Exception
 {
     System.out.println(newList);
 }
public   LinkedList<myServiceOrder> createServiceOrder(File inFile) throws Exception {
    int number=0;
     String ownerName="";
     String make="";
     String model="";
     int year=0;
     myServiceOrder serviceList = new myServiceOrder();    
        Scanner fileScan=new Scanner(inFile);
            while (fileScan.hasNext())
            {
                String ignore;

                number = fileScan.nextInt();
                //System.out.println(number);
                ignore = fileScan.nextLine(); // ignore the newline
                ownerName = fileScan.nextLine();
            //  System.out.println(ownerName);
                make = fileScan.nextLine();
        //      System.out.println(make);
                model = fileScan.nextLine();
            //  System.out.println(model);
                year = fileScan.nextInt();
        //      System.out.println(year);
                ignore = fileScan.nextLine(); // ignore the newline
                  serviceList = new myServiceOrder( number,  ownerName,  make,        model,  year);         
                  newList.add(serviceList);

            }
        fileScan.close();


     //   System.out.println(newList.viewAll());
        return newList; 
    }


}

好吧,我明白了,我很密集。我还有第二个问题:我必须以三种不同的方式对列表进行排序,具体取决于我在 GUI 中选择的选项,我假设我实现Comparable了 ,但在我的compareTo界面中它是compareTo(Object o, int key). 如果排序方法只是 Object o,我如何使用该键。我应该尝试使用Comparator吗?如果我key=1怎么能告诉它在我的 List 类中以这种方式排序?

4

4 回答 4

7

myServiceOrder 类中缺少覆盖 toString() 方法的经典案例。

在此处查看实施中的示例。此页面Rohit 的回答解释了为什么需要覆盖 toString()。




Argh 直到现在很晚才看到您的第二个问题:

请参阅this questionthis questionComparable关于使用界面与使用界面之间的区别Comparator

于 2012-09-29T22:43:59.983 回答
3

Java 怎么知道您希望如何打印 myService 对象?您可以通过覆盖 toString 来告诉它:

@Override
public String toString() {
    return "myServiceObject#" + number + "[" + ownername + ", " + make + ", " + model + ", " + year + "]";
}
于 2012-09-29T22:48:24.210 回答
1

打印的实际上是您在不覆盖 toString 方法的情况下打印的对象的哈希码。现在由于您正在打印LinkedList,您不能这样做..相反,您可以遍历列表并打印单个元素:-

public  void  Print() throws Exception
 {
    for (myServiceOrder so: newList) {
         System.out.println(so)
    }
 }

现在,因为serviceOrder它本身就是一个对象..你需要toString()在那个类中覆盖你的..

@Override
public String toString() {
    return this.ownerName + this.make + "[" +  this.model + " - " + String.valueOf(this.year) + "]";            
}
于 2012-09-29T22:45:26.243 回答
1
System.out.println(newList);

这会自动调用类的toString()方法,LinkedList然后调用toString()列表中的每个引用(ServiceOrder在这种情况下是您的对象)。由于您没有提供自己的toString()方法,因此使用默认的方法Object。这给出了有趣的输出myServiceOrder@3bc1cac,这是 Java 打印引用变量的默认方式。如果您希望看到其他内容,您需要通过toString()在您的ServiceOrder类中实现来告诉 Java 如何做到这一点。

于 2012-09-29T22:49:06.620 回答