java中@Override的作用

本文详细介绍了 Java 中 @Override 注解的使用方法及其重要性。@Override 用于标记子类中重写父类的方法,有助于防止错误地覆盖方法。若标记的方法未能正确覆盖父类方法,编译器将生成错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=xx-large][b]java中@Override的作用[/b][quote][/size][list]
@Override是java的编译时批注(annotation)语法,

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

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

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

[/list]
@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").
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.


[/quote]
@Override
public String toString(){
return "user"+age;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值