【发布时间】:2019-03-20 17:40:15
【问题描述】:
我下面有一些代码可以打印输出两次。我如何只打印底部两行而不打印car1.print();car2.print();。我相信它必须是super.print();
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake);
type = theType;
model = theModel;
super.print();
{
System.out.println(" type = " + theType);
System.out.println(" Model = " + theModel);
}
}
}
class Task1 {
public static void main(String[] args) {
Car car1 = new Car(1200,"Holden","sedan","Barina");
Car car2 = new Car(1500,"Mazda","sedan","323");
car1.print();
car2.print();
}
}
【问题讨论】:
-
不删除代码,你不能:)
-
{}没有做任何事情。里面的任何东西都只是构造函数的一部分。不太确定您在这里真正需要什么。 -
你的类
Vehicle中有一个方法print(),我猜……你为什么在Car的构造函数中调用它(super.print())和 之后显式打印它们的值(通过car1.print()和car2.print())?只需摆脱其中一个调用...另一个想法是在每个类中提供适当的toString()并打印出来。 -
您能添加实际结果和预期结果吗?
-
您能否将预期的输出也添加到问题中
标签: java printing superclass