聘我网

新概念招聘3.0

java中@Override起到什么作用?

vote up0vote downstar
public class Animal { 
   public void eat() { System.out.println("I eat like a generic Animal."); } 

} 

public class Wolf extends Animal { 
   @Override 
   public void eat() { System.out.println("I eat like a wolf!"); } 
} 

其中的@Override是注释性的还是有具体功能?

 

2 个答复

vote up0vote downcheck

@Override是java的编译时批注(annotation)语法,

作用是标记哪些是重载父类的方法,哪些是自己新增的方法

并且java编译器在发现有此批注的方法并不是父类的方法时会抛出一个error

并不影响运行时,而且大小写敏感,所以不能写成@override等诸如此类

@Override — the @Override annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled "Interfaces and Inheritance").

// mark method as a superclass method     
// that has been overridden 
@Override      
int overriddenMethod() { }  

While it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

链接
vote up0vote down

One @Override just for one method.

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.