我不明白这个错误会被抛出

时间:2019-12-18 17:03:54

标签: java object

很抱歉,如果这是一个非常基本的问题,因为我只是在学习对象,但我不明白此错误的含义。我必须创建一个对象类并使用预制程序对其进行测试,所以我将两者都粘贴。

对象:

 public class Vehicle{
  private int location=0;
  public Vehicle(int loc){
    if(loc<=20||loc>=-20){
      location=loc;
    }else{
      location=0;
    }
  }
  public void forward(){
    if(location<20){
      location++;
    }
  }
  public void backward(){
    if(location>-20){
      location--;
    }
  }
  public int getLocation(){
    return location;
  }
  public String toString(){
    String sloc=new String();
    for(int i=0;i<location+20;i++){
      sloc+=' ';
    }
    sloc+='@';
    return sloc;
  }
}

检查器:

import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;


public class student_runner_Vehicle
{
       public static void main (String str[]) throws IOException {
            Vehicle v1 = new Vehicle (17);

            System.out.println(v1);

            for (int i = 1; i < 5; i ++)
            {
                 v1.forward();
                 System.out.println(v1);
            }
            System.out.println(v1.getLocation());

            for (int i = 1 ; i < 10; i ++)
            {
                 int d = (int)(Math.random() *2);
                 if (d ==0)
                      v1.forward();
                else
                     v1.backward();
                System.out.println(v1);


            }

            Vehicle v2 = new Vehicle (87);
            System.out.println(v2.getLocation());

            Vehicle v3 = new Vehicle(-18);
            System.out.println(v3);
            v3.backward();
            System.out.println(v3.getLocation());
            v3.backward();
            System.out.println(v3.getLocation());
            v3.backward();
            System.out.println(v3.getLocation());
            v3.backward();
            System.out.println(v3.getLocation());

       }

}

这是错误:

Main.java:234: error: constructor Vehicle in class Vehicle cannot be applied to given types;
        Vehicle v1 = new Vehicle ();
                     ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length

我通常能够自己查看这些错误并进行修复,但是我真的不知道该怎么办。 谢谢

1 个答案:

答案 0 :(得分:3)

它告诉您在创建对象时缺少构造函数的参数。具体而言,在这种情况下,它期望一个整数。但是,您提供的代码似乎正确:

std::type_index

您是否有可能进行更改,然后在编译之前忘记保存?

相关问题