简体   繁体   English

方法不会从父类型@override覆盖或实现方法编译错误

[英]method does not override or implement a method from a supertype @override compile error

I'm getting an error method does not override or implement a method from a supertype @Override . 我收到一个错误method does not override or implement a method from a supertype @Overridemethod does not override or implement a method from a supertype @Override I want to print "cannot change capacity of a car" after it prints one car. 我要在打印一辆汽车后打印“无法更改汽车的容量”。 I need to override the setCapacity to print this other part. 我需要重写setCapacity来打印其他部分。 I believe the code is mostly correct, just not sure why it's not correctly override the setCapacity method. 我相信代码大部分是正确的,只是不确定为什么它不能正确覆盖setCapacity方法。 The final output is: 最终输出为:

New capacity = 1600
Vehicle Info:
capacity = 1600cc
make = Mazda
Cannot change capacity of a car
Vehicle Info:
capacity = 1200cc
make = Holden
type = sedan
model = Barina

My code is: 我的代码是:

class Vehicle {  // base class

   public void setCapacity(int setCapacity) {
     this.capacity = setCapacity;
      System.out.println("New Capacity = " + setCapacity);
   }

   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
     public void setCapacity() {
       super.print();
       System.out.println("Cannot change capacity of a car");
     }       
 }

class Task3 {

   public static void main (String[]args){
      Car car1 = new Car (1200,"Holden","sedan","Barina" );
      Vehicle v1 = new Vehicle (1500,"Mazda");
      v1.setCapacity(1600);
      v1.print();
      car1.setCapacity(1600);
      car1.print();
   }
}

There is a mismatch in the child and parents method signature of setCapacity() . setCapacity()setCapacity()方法和父代方法签名不匹配。 If you want to override a method from the parent class in the child class then it must have the same signature. 如果要覆盖子类中父类的方法,则它必须具有相同的签名。

Change 更改

public void setCapacity() { //... }

to

public void setCapacity(int setCapacity) { // ... }

in the Car class. Car课上。

In your code you have missed the parameter setCapacity and thus the compiler complains. 在您的代码中,您错过了参数setCapacity ,因此编译器抱怨。

The void setCapacity(int setCapacity) is not overriden. void setCapacity(int setCapacity)不被覆盖。 void setCapacity() and void setCapacity(int setCapacity) are two different methods. void setCapacity()void setCapacity(int setCapacity)是两种不同的方法。 Therefore generates the @Override annotation the compile error. 因此,生成@Override批注的编译错误。

Regarding the terminology, the setCapacity is said to be overloaded in this scenario. 关于术语,在这种情况下, setCapacity被认为是过载的。

The signature of createCapacity is different in Vehicle and Car classes. 在车辆类和汽车类中,createCapacity的签名不同。 So, there is a compilation error. 因此,存在编译错误。 In Vehicle class you have an argument setCapacity but in the Car class the argument list to the method is empty. 在Vehicle类中,您有一个参数setCapacity,但在Car类中,该方法的参数列表为空。 So, overriding is not possible. 因此,无法覆盖。

@Override
public void setCapacity(   int capacity   ) { --> **adding this argument here will fix the issue.**
    super.print();
    System.out.println("Cannot change capacity of a car");
}

public void setCapacity(int setCapacity) {
    this.capacity = setCapacity;
    System.out.println("New Capacity = " + setCapacity);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 错误:(124,9)错误:方法未覆盖或从超类型实现方法 - Error:(124, 9) error: method does not override or implement a method from a supertype React Native - 错误:方法不会覆盖或实现超类型的方法 - React Native - error: method does not override or implement a method from a supertype Java错误:方法未从超类型重写或实现方法 - java error: method does not override or implement a method from a supertype 错误:方法onEnable不会覆盖或实现超类型的方法 - Error: method onEnable does not override or implement a method from a supertype Java“方法没有覆盖或实现来自超类型的方法”错误 - Java "method does not override or implement a method from a supertype" error 错误:方法未覆盖或从超类型实现方法 - error: method does not override or implement a method from a supertype 错误:方法未覆盖或从超类型OnCreateOptionsMenu实现方法 - error: method does not override or implement a method from a supertype OnCreateOptionsMenu 方法不会从超类型错误中覆盖或实现方法 - method does not override or implement a method from a supertype error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM