如何停止打印我一半的打印方法

时间:2018-10-16 06:40:30

标签: java class inheritance superclass

在停止打印方法方面我只需要一些帮助。其输出我的输出两次为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();
   }
}

4 个答案:

答案 0 :(得分:2)

您可以在构造函数中使用super关键字来调用超类的构造函数并将参数传递给它。 请注意,它必须是构造函数中的第一条语句:

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;
   }
}

答案 1 :(得分:0)

您缺少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;
}

答案 2 :(得分:0)

您必须通过在子类中传递简单的参数来调用超级构造函数。

public Car(int capacity, String make, String type, String model) {
      super(capacity, make); // simply call super
      this.type = type;
      this.model = model;
   } 

答案 3 :(得分:0)

解决方案之一是使用super关键字从子类中调用基类的构造方法,并如@Mureinik所述从子类的构造方法中添加其他参数

根据基类的要求,您还可以尝试使用抽象方法。示例代码如下。

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";
    }

    }