Java 显示方法-输出错误
我开始学习JAVA。我被要求创建一个跟踪新车和二手车的汽车程序。我应该创建一个名为car的超级类,两个名为UsedCar和NewCar的派生类,以及一个测试这3个类的驱动程序类 所有类都编译并运行。然而。当我输出它时,我得到垃圾输出。我不明白我哪里出错了。我知道驾驶舱很好,还有超级“汽车”舱。在UsedCar和NewCar类中的某个地方,它导致输出错误。任何建议都会有帮助 这是我的驾驶课:Java 显示方法-输出错误,java,inheritance,equals,superclass,derived-class,Java,Inheritance,Equals,Superclass,Derived Class,我开始学习JAVA。我被要求创建一个跟踪新车和二手车的汽车程序。我应该创建一个名为car的超级类,两个名为UsedCar和NewCar的派生类,以及一个测试这3个类的驱动程序类 所有类都编译并运行。然而。当我输出它时,我得到垃圾输出。我不明白我哪里出错了。我知道驾驶舱很好,还有超级“汽车”舱。在UsedCar和NewCar类中的某个地方,它导致输出错误。任何建议都会有帮助 这是我的驾驶课: public class CarDriver { public static void main(Str
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.java:11)”为您创建的每个
NewCar
实例创建两个额外的NewCar
s
public class NewCar extends Car
{
// [...]
NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver");
这些车尝试再创建两辆NewCar
s,这将尝试创建另外两辆NewCar
s,依此类推。这只会在达到某个级别之前起作用(您会得到一个StackOverflow
)。如果要避免异常,需要删除两个字段的初始化new1
和new2
在UsedCar
中也有类似的问题
另外,如果要覆盖equals
方法,则签名应该是公共布尔等于(Object UsedCar)
而不是公共布尔等于(Car UsedCar)
Tipp:将@Override
注释添加到应重写超类/接口中方法的每个方法中,如果签名错误,编译器将告诉您
此外,您可能需要将
price
的类型从Double
更改为Double
。如果您知道需要,您应该只使用Double
,而不是Double
。自动装箱和取消装箱可能会降低性能。请看我可以看到新车的一些问题
首先,您正在用代码实例化(创建)两辆新车
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);
尝试删除这些行
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);
从NewCar
类和这些行
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);
从UsedCar
类。您正在类本身内部创建这些类的实例,我认为这不是您想要做的
另外,equals()
的实现也不正确。例如,在NewCar
中
public boolean equals(Car NewCar)
请注意,变量的名称实际上是类的名称。相反,你想要
public boolean equals(Object obj)
此外,在比较变量之前,应该测试所传递的对象是否是适当的
Car
子类的实例。对于UsedCar
类中的equals()
也是如此。在NewCar和UsedCar类中,创建一个递归类
由错误引起的
删除以下内容:
NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver")
及
在你使用的构造器中
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";
}
}
UsedCar.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方法是有意义的。