Netbeans 覆盖注释
我正在尝试创建一个继承抽象超类(包含三个抽象方法)的新类。问题是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为你是压倒一切的(有点 - 见下文)。
添加注释的原因之一是保护自己免受伤害。如果没有注解,如果修改了抽象类并删除了抽象方法,子类中的方法就会在你不知情的情况下变得“正常”。
通过注释,编译器会说“嘿 - 你告诉我你正在覆盖某些东西,但你没有”
Because you are overriding (sort of - see below).
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"
以下是
@Override
说:
我知道这有点令人困惑,因为您实际上并没有覆盖任何东西(您正在实现它!),但这就是它的工作原理。
Here is what the documentation for
@Override
says: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.