构造函数vehichle不能应用于给定的类型?

时间:2015-12-03 12:37:30

标签: java

我已经创建了一个类和两个子类,它们是Van和Bicycle,这两个类继承自Vehicle类,但是一旦我运行代码我就会收到错误(“构造函数的车辆不能应用于给定类型?required(int,String ...)“)

import java.io.FileNotFoundException;
import java.util.Scanner;


import java.io.*;

public class Schedule {
public static void executeTask(int option){
System.out.println("i am in executeTask");
            char addmore='n'; 
            switch(option){
            case 0: 
                    return; 
            case 1: 
                    do{
                        add();
                        addmore = getContinue("add");
                        if(addmore=='n')
                            break;
                    }while(true); 
                    break;
case 2: 
                do{
                    view();
                    addmore = getContinue("view");
                    if(addmore=='n')
                        break;
                }while(true); 
                break;
            }
        }
         public static char getContinue(String methodName){
             char ch='n'; 
             try{
                    System.out.println("Do you want to " +methodName + " more records (y/n)?"); 
                    ch = (char) System.in.read();

                }catch(IOException e){
                    System.out.println("IOException in input....");
                }
             return ch;
         }
        public static void add() {
            char ch; 
            System.out.println("Add Record");
            System.out.println("---------------");
            System.out.println();
            File file = new File("readcoursefile.txt");//Here you need to provide proper path.You just go to your file location and copy the path and paste here

                try {

                    Scanner scanner = new Scanner(file);

                    while (scanner.hasNextLine()) {
                        String line = scanner.nextLine();
                        System.out.println(line);
                    }
                    scanner.close();
                } catch (FileNotFoundException e) {
                    System.out.println("file not found");
                }

            try{
                System.out.println("Do you want to save/cancel record s/c"); 
                ch = (char) System.in.read();

            }catch(IOException e){
                System.out.println("IOException in input....");
            }



        }

        public static void update(){
            System.out.println("Update Record");
            System.out.println("---------------");
            System.out.println();


        }

        public static void view(){
            System.out.println("View Record");
            System.out.println("---------------");
            System.out.println();



        }

        public static void delete(){
            System.out.println("Delete Record");
            System.out.println("---------------");
            System.out.println();

            //prompt user for console input (attributes)

            //write/update user record from file.

        }

        public static void search(){
            System.out.println("Search Record");
            System.out.println("---------------");
            System.out.println();



        }

public static void main(String []args)
{
Schedule sh=new Schedule();
System.out.println("i am in main");
sh.executeTask(1);
}


    }

2 个答案:

答案 0 :(得分:2)

您的Vehicle构造函数接受三个参数,但是您尝试从扩展Vehicle的类中提供四个参数。您有以下内容:

public Vehicle(int maxSpeed, String make, double regNumber) {
        this.maxSpeed = maxSpeed;
        this.make = make;
        this.regNumber = regNumber;
    }

但你尝试通过

调用它
super(maxSpeed, make, regNumber, multipleGeers);

超级中的最后一个论点是问题。如果你删除它然后它将成为三个args并匹配超级构造函数。如果您希望添加额外的,那么您需要修改超级Vehicle构造函数以包含第4个参数。

可能的解决方案,将调用更改为超级构造函数,如下所示:

super(maxSpeed, make, regNumber);

或者将s Vehicle构造函数更改为接受另一个值,如下所示:

public Vehicle(int maxSpeed, String make, double regNumber, int multipleGeers) {
        this.maxSpeed = maxSpeed;
        this.make = make;
        this.regNumber = regNumber;
        //use multipleGeers here 
    }

答案 1 :(得分:0)

在构造函数中,您传递了3个参数:

  public Vehicle(int maxSpeed, String make,
      double regNumber)
      {
        this.maxSpeed= maxSpeed;
        this.make = make;
        this.regNumber = regNumber;
      }

在调用构造函数时,您传递了4个参数:

super(maxSpeed, make, regNumber, multipleGeers);

因此,请考虑删除额外的参数:

super(maxSpeed, make, regNumber);