简体   繁体   English

在java中可以覆盖方法的不同方法是什么?

[英]what are the different ways in which a method can be overridden in java?

编辑:我的问题是什么不同的访问修饰符,返回类型和参数列表可以在子类方法中使用,以便重写具有相同名称的超类方法?

Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters. 当同一类中的两个或多个方法具有完全相同的名称但参数不同时,会发生Java中的方法重载。

But, i suppose you mean override since you're talking about inheritance : 但是,我想你的意思是覆盖,因为你在谈论继承:

In Java, a method is only overrided when a subclass has a method with the same signature . 在Java中,只有当子类具有相同签名的方法时才会覆盖方法。

A method signature consists only of the name of the method and the parameters types and their order . 方法签名仅包含方法名称参数类型及其顺序

You cannot overload a method by changing the return value (although you can override and specialize it) or by access modifiers (although you can override it and make it "more accessible"). 您不能通过更改返回值(尽管可以覆盖和专门化它)或通过访问修饰符来重载方法(尽管您可以覆盖它并使其“更易于访问”)。 Only the argument list can be used to overload a method. 只有参数列表可用于重载方法。

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

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