@Override注解,不是关键字,但可以当关键字使用,可以选择添加这个注解,在你不留心重载而并非复写了该方法时,编译器就会产生一条错误:The method doh(Milhouse) of type Bart must override or implement a supertype method

// reusing/hide.java
//owrloading a base method name in a derived
package object; import static net.mindview.util.Print.*; class Homer{
char doh(char c)
{
print("doh(char)"); return 'd';
}
float doh(float f)
{
print("doh(float");
return 1.f;
}
}
class Milhouse{}
class Bart extends Homer{
void doh(Milhouse m)
{
print("doh(Milhouse");
}
@Override char doh(char c) //基类必须也有char doh(char c)方法
{
print("I have not heavy load");
return 'a';
}
} public class Hide {
public static void main(String[] args)
{
Bart b = new Bart();
b.doh(1);
b.doh('x');
b.doh(1.0f);
b.doh(new Milhouse());
}
}/* output:
doh(float
I have not heavy load
doh(float
doh(Milhouse
*///~
05-11 22:49