Netbeans 覆盖注释

发布于 2024-12-10 12:08:04 字数 1081 浏览 0 评论 0原文

我正在尝试创建一个继承抽象超类(包含三个抽象方法)的新类。问题是 netbeans 给了我一个警告:添加 @override 注释。如果我不重写任何方法,为什么我应该这样做(添加此注释)。有什么问题吗?

超类是

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);
   }
}

子类是

 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");

   }

}

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. 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");

   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-12-17 12:08:04

因为你是压倒一切的(有点 - 见下文)。

public class Avion extends Vehicul {

     public void mediulDeDeplasareAVehiculului() {
     ...
}

添加注释的原因之一是保护自己免受伤害。如果没有注解,如果修改了抽象类并删除了抽象方法,子类中的方法就会在你不知情的情况下变得“正常”。

通过注释,编译器会说“嘿 - 你告诉我你正在覆盖某些东西,但你没有”

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"

ˇ宁静的妩媚 2024-12-17 12:08:04

以下是 @Override说:

表示方法声明旨在覆盖 a 中的方法声明
超类。如果一个方法用此注释类型注释但没有覆盖
超类方法,编译器需要生成错误消息。

我知道这有点令人困惑,因为您实际上并没有覆盖任何东西(您正在实现它!),但这就是它的工作原理。

Here is what the documentation for @Override says:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文