复制构造函数未正确输出

时间:2017-04-08 18:31:18

标签: java constructor copy

我将一个对象设置为等于另一个调用复制构造函数的对象。我的问题是,当我测试两个是否等于另一个时,我的输出表明它们不是。一种方法是通过重载的构造函数,另一种方法来自复制构造函数,它们都输出相同的东西。

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 个答案:

答案 0 :(得分:-1)

默认情况下(在java.lang.Object中定义),只有当对象是同一个实例时,它才等于另一个对象。但是,当您覆盖它时,您可以提供自定义相等逻辑。

这意味着standCopy.equals(x)将为false,除非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功能将按预期工作。

String类会覆盖对象类的equals方法。这就是下面的代码打印true

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

如果in-case String类没有覆盖equals方法,那么结果将是false