2017-04-08 16 views
0

我設置一個對象等於另一個調用複製構造函數。我的問題是,當我測試兩個是否等於另一個時,我的輸出說他們不是。一種方法是通過重載構造函數,另一種方法是從複製構造函數中輸出相同的東西。複製構造函數不能正確輸出

public class HotDogClass { 

    private int standNumber; // ID number for hotdog stand 
    private int soldToday; //dogs each stand sold 
    private static int totalSales; 

    public void setstandNumber (int newstandNumber) { standNumber =  newstandNumber; } 
    public int getstandNumber () { return standNumber; } 
    public int getsoldToday() { return soldToday; } 
    public void setsoldToday (int st) { soldToday = st; 
     if (soldToday <= 0) 
     System.out.println("no hotdogs sold");} 
    public static int gettotalSales () { return totalSales; } 


    static { 
     totalSales=0; 
     System.out.println("Static initializer block\n"); 
    } 
    public HotDogClass() { 
     this (0, 0); 
     System.out.println("Default constructor"); 
    } 

    public HotDogClass (int sta, int st) { 
     standNumber = sta; 
     soldToday = st; 
     totalSales += soldToday; 
     System.out.println("overloaded contructor"); 
    } 
    public HotDogClass (HotDogClass original) { 
     this.standNumber = original.standNumber; 
     this.soldToday = original.soldToday; 
     //totalSales += soldToday; 
     System.out.println("Copy contructor"); 
    } 
    public void justSold () { 
     soldToday++; 
     totalSales++; 
    } 

    public String toString (){ 
     String x = "Stand number " + standNumber + " sold " + soldToday + " hotdogs" ; 
     return x; 
    } 


    public void dispose () { 
     System.out.println("HotDogStand - dispose method"); 
    } 
    public void finalize () { 
     System.out.println("HotDogStand - finalize method"); 
    } 
} 

public class HotDogStand { 

    public static void main(String[] args) { 

     HotDogClass stand1 = new HotDogClass(21, 10); 
     System.out.println (stand1 + " \n " ); 
     //stand1.justSold(); 
     //stand1.justSold(); 
     HotDogClass standCopy = new HotDogClass (stand1); 
     System.out.println(standCopy + "\n"); 
     if (standCopy.equals(stand1)) 
      System.out.println("Stand1 and Standcopy are equal\n"); 
     else 
      System.out.println("Stand1 and standcopy are not equal\n"); 

     HotDogClass stand2 = new HotDogClass(32, 7); 
     System.out.println (stand2 + " \n "); 
     stand2.justSold(); 
     stand2.justSold(); 
     stand2.justSold(); 

     HotDogClass stand3 = new HotDogClass(); 
     System.out.println(stand3 + " \n "); 


     HotDogClass stand4 = new HotDogClass(); 
     stand4.setsoldToday(0); 
     stand4.setstandNumber(10); 
     System.out.println(stand4 + " - Testing my set and get methods\n"); 

     System.out.println("The ending total of all hotdogs sold is " 
       + HotDogClass.gettotalSales()); 

    } 
} 
+1

你應該好好學習基本的Java編碼標準,如命名('getSoldToday()' ,而不是'getsoldToday()'),使用'@ Override',並且當一個基本的初始化器賦值工作時不使用靜態塊'private static int totalSales = 0'。但最根本的是,你永遠不會重寫'equals'。 – chrylis

+0

你並不是首先強調平等。請學習重複的問題/答案,以瞭解如何正確地做到這一點! – GhostCat

+0

不要在構造函數中放入'println'調用。 –

回答

-1

默認情況下(在java.lang.Object中定義),一個對象只有在它是相同的實例時纔等於另一個對象。但是當你覆蓋它時你可以提供自定義的相等邏輯。

這意味着standCopy.equals(x)將是錯誤的,除非x是standCopy本身。

你需要在你的類中實現一個equals方法。

像這樣的事情

@Override 
public boolean equals(Object o) { 
    if(o instanceof Hotdog){ 
     Hotdog h = (Hotdog)o; 
     return ((this.standNumber).equals(h.standNumber)) && ((this.soldToday).equals(h.soldToday)) 
    } 
    return false; 
} 

現在你equals功能將正常工作。

字符串類覆蓋對象類的equals方法。這就是爲什麼下面的代碼打印true的原因。

String aString = new String("TEST"); 
String anotherString = new String("TEST");  
System.out.println(aString.equals(anotherString)); 

如果情況String類就不會覆蓋了equals方法,那麼結果將是false

+0

它實際上是'equals(Object o)' – QBrute

+0

更正了錯誤。感謝您指出 –