简体   繁体   English

Netbeans覆盖注释

[英]Netbeans Override Annotation

i'm trying to create a new class that inherits from an abstract superclass (contains three abstract methods). 我正在尝试创建一个从抽象超类继承的新类(包含三个抽象方法)。 The issue is that netbeans gives me a warning : add @override annotation. 问题是netbeans给了我一个警告:添加@override注解。 why should i do this (add this annotation) if i'm not overriding any method. 如果我不重写任何方法,为什么要这样做(添加此批注)。 What's the problem ? 有什么问题 ?

Superclass is 超类是

abstract class Vehicul {

   String denumireaVehiculului;
   float lungimeaMinimaVehicul;
   int numarulMinimDeLocuri;

   public abstract void mediulDeDeplasareAVehiculului();
   public abstract void nivelulDeSiguranta();

   // Constructor implicit
   Vehicul() {

       denumireaVehiculului = "Eu sint vehicul";
       System.out.println(denumireaVehiculului);

       lungimeaMinimaVehicul = 3.50f;
       System.out.println("Lungimea minima este "+lungimeaMinimaVehicul);

       numarulMinimDeLocuri = 2;
       System.out.println("Numarul minim de locure este "+numarulMinimDeLocuri);
   }
}

Subclass is 子类为

 public class Avion extends Vehicul {

 public void mediulDeDeplasareAVehiculului() {

}

 public  void  nivelulDeSiguranta() {

}

public String getDenumireaVehiculului() {

     return "Avion";

public void   afiseazaCineEsti() {

        System.out.println("Eu sunt un avion");

   }

} }

Because you are overriding (sort of - see below). 因为您要覆盖(排序-如下所示)。

public class Avion extends Vehicul {

     public void mediulDeDeplasareAVehiculului() {
     ...
}

One reason for adding the annotation is to protect yourself from yourself. 添加注释的原因之一是保护自己免受自己的伤害。 Without the annotation if the abstract class were modified and the abstract method removed, the method in the subclass would become "normal" without you knowing it. 如果没有注释,则如果修改了抽象类并删除了抽象方法,则子类中的方法将变为“正常”而您不知道。

With the annotation, the compiler will say "Hey - you're telling me you're overriding something, but you're not" 带有注释,编译器会说“嘿-您是在告诉我您覆盖了某些内容,但您不是”

Here is what the documentation for @Override says: 这是@Override的文档所说的内容:

Indicates that a method declaration is intended to override a method declaration in a superclass. 指示方法声明旨在覆盖超类中的方法声明。 If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message. 如果使用此注释类型对方法进行注释但未覆盖超类方法,则要求编译器生成错误消息。

I know it is a bit confusing, because you are not actually overriding anything (you are implementing it!), but that is just how it works. 我知道这有点令人困惑,因为您实际上并没有覆盖任何东西(您正在实现它!),但这就是它的工作方式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM