有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从两个类调用方法

下面是一个Java汽车程序,我可以在其中存储模型,制作等。。。我想添加一个名为VehicleDB的新类,它通过addVehicle方法将车辆或汽车添加到数据库中。然后我想有一个方法,通过VehicleDB类中的print方法将数据库中的所有车辆打印出来。我将如何引用Vehicle中现有的两种原始打印方法和VehicleDB中的类?谢谢

 class Vehicle {
       int capacity;
       String make;
       int setCapacity;

       Vehicle(int theCapacity, String theMake) {
          capacity = theCapacity;
          make = theMake;
       }

       int setCapacity(int setCapacity){
          capacity = setCapacity;
          System.out.println("New capacity = " + setCapacity);
          return setCapacity;
       }

       void print() {
          System.out.println("Vehicle Info:");
          System.out.println("  capacity = " + capacity + "cc" );
          System.out.println("  make = " + make );
       }
    }

    class Car extends Vehicle {
       String type;
       String model;

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

       Car(int theCapacity, String theMake, String theType, String theModel){
          super(theCapacity, theMake);
          this.type = theType;
          this.model = theModel;
       }

       @Override
       int setCapacity(int setCapacity){
          System.out.println("Cannot change capacity of a car");
          return capacity;
       }

    }

    class VehicleDB {
       void addVehicle(Vehicle Vehicle){

       }

       void print(){
          System.out.println("=== Vehicle Data Base ===");
       }
    }

    class Task4 {

       public static void main(String[] args) {
          VehicleDB db = new VehicleDB();
          db.addVehicle(new Car(1200,"Holden","sedan","Barina"));
          db.addVehicle(new Vehicle(1500,"Mazda"));
          db.print();
       }
    }

共 (0) 个答案