简体   繁体   English

如何覆盖 Java 中的方法

[英]How to Override A Method in Java

I play Robocode (it is a java programming game).我玩 Robocode(这是一个 Java 编程游戏)。 I want to change the method我想改变方法

public void onScannedRobot (ScannedRobotEvent e) {

}

to

public int onScannedRobot(ScannedRobotEvent e){
}

So I can adapt my robot more if it scanned is true or false.因此,如果扫描的是真还是假,我可以更多地适应我的机器人。

I heard that it is possible with Overriding a method.我听说重写方法是可能的。 So I tryed it:所以我试了一下:

I wrote:我写:

public void onScannedRobot(ScannedRobotEvent e) {

    public int onScannedRobot(ScannedRobotEvent e) {
        return 1;
    }
}

But it won't work但它不会工作

This won't work because the return type of overriden method should be the same or a subtype of the return type declared in the original method.这将不起作用,因为重写方法的返回类型应该与原始方法中声明的返回类型相同或子类型。 Look at this link about overriding and this about overloading看看这个关于覆盖和这个关于重载的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM