【问题标题】:Display Method - Output Error显示方法 - 输出错误
【发布时间】:2015-06-23 08:32:56
【问题描述】:

我开始学习JAVA。我被要求创建一个跟踪新车和二手车的汽车程序。我应该创建一个名为 car 的超类,两个名为 UsedCar 和 NewCar 的派生类,以及一个测试这 3 个类的 Driver 类。

所有类都编译并运行。然而。当我输出它时,我得到垃圾输出。我不明白我哪里出错了。我知道 Driver 类很好,还有超级“Car”类。在 UsedCar 和 NewCar 类的某个地方,它会导致输出错误。任何意见或建议都会有所帮助。

这是我的驱动程序类:

public class CarDriver
{

public static void main(String[] args)
{
  NewCar new1 = new NewCar(8000.33, "silver");
  NewCar new2 = new NewCar(8000.33, "silver");
  if (new1.equals(new2))
  {
    new1.display();
  }

  UsedCar used1 = new UsedCar(2500, 100000);
  UsedCar used2 = new UsedCar(2500, 100000);
  if (used1.equals(used2))
  {
    used1.display();
  }
} // end main
}//end class

这是我的汽车类:

import java.util.*;


public class Car
{

//Variables

public Double price;


//Constructor

public Car(Double cost)//constructor to create instances of SavingsAccount
            {
                price = cost *2;
            }

//GetPrice method

public Double getPrice()//method to get the cars' price
            {
            return price;//returns the value of the price

        }


    }//end class Car

这里是派生类:NewCar

import java.util.*;

public class NewCar extends Car
{

    //Variables
    public String color = "silver";

NewCar new1 = new NewCar(8000.33, "silver");
  NewCar new2 = new NewCar(8000.33, "silver");

    //Constructor - Two Parameter

    public NewCar (Double price, String color)//constructor to create instances of new car
                {
                    super(price);
                    color = this.color;

            }


    //Equals Method

    public boolean equals(Car NewCar)
    {
      if (NewCar == null)
      {
        return false;
      }
      else
      {
        return
          price.equals(new1.price) &&
          color.equals(new2.color);
      }
} // end equals

//Display method

public void display ()
{
    System.out.println(" " + new1.price + new1.color);
}//end display method

}//end class NewCar

二手车

import java.util.*;

public class UsedCar extends Car
{

//Variables

private double mileage;
public String color = "silver";

UsedCar used1 = new UsedCar(2500, 100000);
 UsedCar used2 = new UsedCar(2500, 100000);

//Constructor -Two Parameter

public UsedCar (double price, double mileage)//constructor to create instances of new car
                {
                    super(price);
                    mileage = this.mileage;

            }

  //Equals Method

 public boolean equals(Car UsedCar)
    {
      if (UsedCar == null)
      {
        return false;
      }
      else
      {
        return
          price.equals(used1.price) &&
          color.equals(used2.color);
      }
} // end equals

//Display method

public void display ()
{
    System.out.println(" " + used1.price + used1.mileage);
}//end display

}//end class

我无法粘贴我的输出,但它在命令行上看起来像这样并且它会不停地继续:

“在 NewCar .(NewCar.java:11)”

【问题讨论】:

    标签: java inheritance equals superclass derived-class


    【解决方案1】:

    您为每个创建的 NewCar 实例创建 2 个额外的 NewCars

    public class NewCar extends Car
    {
    // [...]
    
    NewCar new1 = new NewCar(8000.33, "silver");
    NewCar new2 = new NewCar(8000.33, "silver");
    

    这些汽车尝试再创建 2 个NewCars,这将尝试再创建 2 个NewCars,依此类推。这只会在你达到一定水平之前有效(你得到一个StackOverflow)。如果要避免异常,则需要删除 new1new2 2 个字段的初始化。

    你在UsedCar也有类似的问题。

    另外,如果您想覆盖equals 方法,签名应该是public boolean equals(Object UsedCar) 而不是public boolean equals(Car UsedCar)

    提示:@Override 注释添加到应该覆盖超类/接口中的方法的每个方法中,如果您的签名错误,编译器会告诉您。


    您可能还想将price 的类型从Double 更改为double。如果你知道你需要它,你应该只使用Double 而不是double。自动装箱和拆箱可能会降低您的性能。见Autoboxing and Unboxing

    【讨论】:

      【解决方案2】:

      我可以立即看到 NewCar 的一些问题。

      首先,您正在使用代码实例化(创建)两个 NewCars

      NewCar new1 = new NewCar(8000.33, "silver");
      NewCar new2 = new NewCar(8000.33, "silver");
      

      正如您所做的那样,在主程序中创建这些是正确的。在类本身中创建两个新的本地实例可能是不正确的。

      其次,你的 equals 语句是 equals(Car NewCar). 我猜你想要 equals(Car otherCar). (请注意实例变量的小写字母作为 Java 约定。)

      那么在你的回报中你会说类似

      return (otherCar.getPrice() == this.getPrice()) && (otherCar.color.equalsIgnoreCase(this.color);
      

      【讨论】:

        【解决方案3】:

        尝试删除这些行

        NewCar new1 = new NewCar(8000.33, "silver");
        NewCar new2 = new NewCar(8000.33, "silver");
        

        来自NewCar 类和这些行

        UsedCar used1 = new UsedCar(2500, 100000);
        UsedCar used2 = new UsedCar(2500, 100000);
        

        来自UsedCar 类。您正在类本身内创建这些类的实例,我认为这不是您想要做的。

        另外,您对equals() 的实现不正确。例如,在NewCar 你有

        public boolean equals(Car NewCar)
        

        请注意,变量的名称实际上是类的名称。相反,您想要

        public boolean equals(Object obj)
        

        此外,在比较变量之前,您应该测试被传递的对象是适当的Car 子类的实例。 UsedCar 类中的 equals() 也是如此。

        【讨论】:

        • @fabian 感谢您编辑我的答案以改进格式。
        【解决方案4】:

        在 NewCar 和 UsedCar 类中创建一个递归类。

        由错误引起。

        删除:

        NewCar new1 = new NewCar(8000.33, "silver");
        NewCar new2 = new NewCar(8000.33, "silver")
        

        UsedCar used1 = new UsedCar(2500, 100000);
        UsedCar used2 = new UsedCar(2500, 100000);
        

        在你使用的 costructor 中

        color = this.color;
        

        实际上你应该使用的时候

        this.color = color.
        

        在方法中等于你实现的太不正确了。

        没错:

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (!super.equals(obj))
                return false;
            if (getClass() != obj.getClass())
                return false;
            NewCar other = (NewCar) obj;
            if (color == null) {
                if (other.color != null)
                    return false;
            } else if (!color.equals(other.color))
                return false;
            return true;
        }
        

        正确的类

        汽车.java

        public class Car {
        
            private Double price;
            private String color;
        
            public Car(String color, Double cost) {
                this.color = color;
                this.price = cost * 2;
            }
        
            public Double getPrice() {
                return price;
            }
        
            public String getColor() {
                return color;
            }
        
            public void setColor(String color) {
                this.color = color;
            }
        
            @Override
            public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((color == null) ? 0 : color.hashCode());
                result = prime * result + ((price == null) ? 0 : price.hashCode());
                return result;
            }
        
            @Override
            public boolean equals(Object obj) {
                if (this == obj)
                    return true;
                if (obj == null)
                    return false;
                if (getClass() != obj.getClass())
                    return false;
                Car other = (Car) obj;
                if (color == null) {
                    if (other.color != null)
                        return false;
                } else if (!color.equals(other.color))
                    return false;
                if (price == null) {
                    if (other.price != null)
                        return false;
                } else if (!price.equals(other.price))
                    return false;
                return true;
            }
        
            @Override
            public String toString() {
                return "Car: \nColor:" + color + "\nPrice: " + price;
            }
        
            public void display() {
                System.out.println(toString());
            }
        
        }
        

        NewCar.java

        public class NewCar extends Car {
        
            private String color = "silver";
        
            public NewCar(String color, Double coast) {
                super(color, coast);
                this.color = color;
            }
        
            @Override
            public int hashCode() {
                final int prime = 31;
                int result = super.hashCode();
                result = prime * result + ((color == null) ? 0 : color.hashCode());
                return result;
            }
        
            @Override
            public boolean equals(Object obj) {
                if (this == obj)
                    return true;
                if (!super.equals(obj))
                    return false;
                if (getClass() != obj.getClass())
                    return false;
                NewCar other = (NewCar) obj;
                if (color == null) {
                    if (other.color != null)
                        return false;
                } else if (!color.equals(other.color))
                    return false;
                return true;
            }
        
            @Override
            public String toString() {
                return super.toString() + "\nType: New\nMileage:0\n";
            }
        }
        

        二手车.java

        public class UsedCar extends Car {
        
            private double mileage;
            private String color = "silver";
        
            public UsedCar(String color, double price, double mileage) {
                super(color, price);
                this.mileage = mileage;
            }
        
            @Override
            public int hashCode() {
                final int prime = 31;
                int result = super.hashCode();
                result = prime * result + ((color == null) ? 0 : color.hashCode());
                long temp;
                temp = Double.doubleToLongBits(mileage);
                result = prime * result + (int) (temp ^ (temp >>> 32));
                return result;
            }
        
            @Override
            public boolean equals(Object obj) {
                if (this == obj)
                    return true;
                if (!super.equals(obj))
                    return false;
                if (getClass() != obj.getClass())
                    return false;
                UsedCar other = (UsedCar) obj;
                if (color == null) {
                    if (other.color != null)
                        return false;
                } else if (!color.equals(other.color))
                    return false;
                if (Double.doubleToLongBits(mileage) != Double.doubleToLongBits(other.mileage))
                    return false;
                return true;
            }
        
            @Override
            public String toString() {
                return super.toString() + "\nType: Used\nMileage: " + mileage + "\n";
            }
        
        }
        

        CarDriver.java

        public class CarDriver {
        
            public static void main(String[] args) {
                Car new1 = new NewCar("silver", 8000.33);
                Car new2 = new NewCar("silver", 8000.33);
                if (new1.equals(new2)) {
                    new1.display();
                }
        
                Car used1 = new UsedCar("silver", 2500, 100000);
                Car used2 = new UsedCar("silver", 2500, 100000);
                if (used1.equals(used2)) {
                    used1.display();
                }
            }
        }
        

        【讨论】:

        • 哇哦。我了解在每个类中创建新对象时我做错了什么。我绝对需要回去更多地研究这一章!此外,这里解释的这些概念比书中的要好。现在重写 equals 方法是有意义的。
        猜你喜欢
        • 1970-01-01
        • 2019-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多