如何停止打印我的打印方法的一半
How to stop printing half of my print method
我只是需要一些帮助来停止打印方法。它以 car1.print() 的形式打印我的输出两次; car2.print();在底部的打印方法中。我如何在不删除它的情况下排除它。它必须放在 super.print() 部分。
class Vehicle { // base class
int capacity;
String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
}
}
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();
}
}
您可以在构造函数中使用super
关键字来调用超级class'构造函数并向其传递参数。
注意必须是构造函数中的第一条语句:
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake); // Here
type = theType;
model = theModel;
}
}
您缺少 Constructor
public Car (int theCapacity, String theMake, String theType, String theModel) {
capacity = theCapacity;
make = theMake;
Type = theType;
Model = theModel;
}
或
public Car (int theCapacity, String theMake, String theType, String theModel) {
super (theCapacity, theMake);
Type = theType;
Model = theModel;
}
您必须通过在子 class 中简单地传递参数来调用超级构造函数。
public Car(int capacity, String make, String type, String model) {
super(capacity, make); // simply call super
this.type = type;
this.model = model;
}
其中一个解决方案是使用 super 关键字从子 class 调用基础 class 构造函数,并添加来自子 class 构造函数的其他参数,如 @Mureinik[=11= 所述]
根据Base的要求Class你也可以尝试使用抽象方法。示例代码如下。
abstract class Vehicle {
static int capacity;
static String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
protected static void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
// you can use these methods where you want in this base class.
System.out.println(" type = " + getType());
System.out.println(" model = " + getModel());
}
protected abstract String getType();
protected abstract String getModel();
}
public class Car extends Vehicle{
Car(int theCapacity, String theMake) {
super(theCapacity, theMake);
}
/**
* @param args
*/
public static void main(){
print();
}
@Override
protected String getType() {
// TODO Auto-generated method stub
return "Audi";
}
@Override
protected String getModel() {
// TODO Auto-generated method stub
return "Q7";
}
}
我只是需要一些帮助来停止打印方法。它以 car1.print() 的形式打印我的输出两次; car2.print();在底部的打印方法中。我如何在不删除它的情况下排除它。它必须放在 super.print() 部分。
class Vehicle { // base class
int capacity;
String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
}
}
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();
}
}
您可以在构造函数中使用super
关键字来调用超级class'构造函数并向其传递参数。
注意必须是构造函数中的第一条语句:
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake); // Here
type = theType;
model = theModel;
}
}
您缺少 Constructor
public Car (int theCapacity, String theMake, String theType, String theModel) {
capacity = theCapacity;
make = theMake;
Type = theType;
Model = theModel;
}
或
public Car (int theCapacity, String theMake, String theType, String theModel) {
super (theCapacity, theMake);
Type = theType;
Model = theModel;
}
您必须通过在子 class 中简单地传递参数来调用超级构造函数。
public Car(int capacity, String make, String type, String model) {
super(capacity, make); // simply call super
this.type = type;
this.model = model;
}
其中一个解决方案是使用 super 关键字从子 class 调用基础 class 构造函数,并添加来自子 class 构造函数的其他参数,如 @Mureinik[=11= 所述]
根据Base的要求Class你也可以尝试使用抽象方法。示例代码如下。
abstract class Vehicle {
static int capacity;
static String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
protected static void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
// you can use these methods where you want in this base class.
System.out.println(" type = " + getType());
System.out.println(" model = " + getModel());
}
protected abstract String getType();
protected abstract String getModel();
}
public class Car extends Vehicle{
Car(int theCapacity, String theMake) {
super(theCapacity, theMake);
}
/**
* @param args
*/
public static void main(){
print();
}
@Override
protected String getType() {
// TODO Auto-generated method stub
return "Audi";
}
@Override
protected String getModel() {
// TODO Auto-generated method stub
return "Q7";
}
}