2012-08-02 42 views
0

所以我知道有關重寫「的toString」看你想要的,而不是類型是什麼東西,但在我的情況下,總的情況是:的toString

Project1:ClassA使用Project2:ClassB那裏我包括Project2分成Project1類路徑和編譯工作正常。但是,Project2:ClassB.toString()打印project2.package.classC而不是classB的classC元素。

請注意,一旦我將classB複製到project1中,打印工作正常,我會看到這些元素。

這裏是CLASSB從項目2快照,我使用我目前PROJECT1:

package edu.cs; 
public class FeatureWeightArrayWritable implements Writable {//classB 

    public int vectorSize; 
    public FeatureWeight[] vector; //classC = FeatureWeight 

    @Override 
    public String toString() { 
     StringBuilder buffer = new StringBuilder(); 
     for (int i = 0; i < this.vectorSize; i++) 
      buffer.append(vector[i].feature + " " + vector[i].weight); 
     // return buffer.toString(); 
     return "Can't see this !!! I see [email protected] "; 
    } 
} 

如果你是可疑FeatureWeight類,我也重寫其toString,但它不應該是即使我使用了上面的返回消息也是如此。任何線索?

這是我如何讀它:

Reader reader = new SequenceFile.Reader(fs, inputPath, conf); 

Writable key = (Writable) reader.getKeyClass().newInstance(); 
FeatureWeightArrayWritable value = new FeatureWeightArrayWritable(); 

while (reader.next(key, value))  
    System.out.println("key:" + key.toString() + ", value:" + value.toString()); 
+0

「toString()'被調用的代碼在哪裏? – 2012-08-02 23:13:17

+0

[email protected]看起來完全像toString()的默認實現。考慮到它打印edu.cs.FeatureWeight而不是edu.cs.FeatureWeightArrayWritable @ ?????我會說你不是在正確的對象上調用它。您是否正在使用(投射到)FeatureWeight類的某處? 你得到的實際輸出是多少?什麼是它的代碼? – 2012-08-02 23:17:22

+0

你確定你正在打印正確的東西嗎? – oldrinb 2012-08-02 23:21:54

回答

0

檢查您的類路徑,並確保你沒有的FeatureWeightArrayWritable另一個副本(舊的類文件,jar文件,第3項目等)周圍鋪設某處。

+0

謝謝大家,理查德似乎是對的,因爲它工作正常,當我「提取Runnable Jar「在eClipse中,但上面的錯誤是使用ant和我定義的build.xml。我仍然沒有發現我的build.xml中的其他副本在哪裏。 – Maha 2012-08-03 02:36:46