可以更改的java方法

时间:2018-10-17 06:13:00

标签: java methods override

我对如何使这种方法不清楚。它必须创建一个名为setCapacity的方法,以后可以覆盖它。我必须“修改基础类的Vehicle类,以包含一个名为setCapacity的方法, 允许更改发动机容量”

class Vehicle {
      void setCapactiy1 () {
         int setCapactity == 0;
      }
   }      
   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;
   }

   @Override
   public void print() {
      super.print();
      System.out.println("  type = " + type);
      System.out.println("  model = " + model);
   }

   @Override

}

class Task2 {

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

1 个答案:

答案 0 :(得分:1)

似乎像一个基本的二传手...它应该像

public void setCapacity(int newCapacity) {
    this.capacity = newCapacity;
}