简体   繁体   English

Java覆盖特定方法a()

[英]Java override specific method a()

How do I chose which function to override?如何选择要覆盖的功能?
There are 2 functions called a() but I only want to override the one with the int(or string).有两个名为 a() 的函数,但我只想用 int(或字符串)覆盖一个。

Code:代码:

public class SuperClass {

    public void a(String string) {
        //Do something
    }

    public void a(int integer) {
        //Do something
    }

}
public class NotSoSuperClass extends SuperClass {

    @Override
    public void a(/*Only for the string*/) {
        //Do something else
    }

}

You go for the method to override:你去覆盖的方法:

@Override
public void a(String string) {
 ...

Done.完毕。 You simply do not override the other method.您只是覆盖其他方法。

You can simply do it by specifying the parameter of type which you want to override like this您可以通过指定要覆盖的类型参数来简单地做到这一点

 @Override
 public void a(String string) {
        //Do something else
 }

or或者

 @Override
 public void a(int integer) {
        //Do something else
 }

It should look like this:它应该是这样的:

@Override
public void a(String string) {
    //Do something else
}

a(string) and a(int) are totally different functions to the compiler. a(string) 和 a(int) 对编译器来说是完全不同的函数。 Overriding one should not affect the other.覆盖一个不应该影响另一个。

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

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