【发布时间】:2015-06-30 21:53:45
【问题描述】:
只是试图澄清重载和覆盖方法之间的区别......考虑下面的场景;
假设我有一堂课,比如说“班主任”。假设我有子类'class Apprentice',这两个类通过继承相关。
public class Apprentice extends Master
假设主类包含 2 个 void 方法,每个方法都命名为攻击,一个采用 Single string 参数,另一个采用 String 和 Int 参数。
public class Master{
void attack(String bodyPart){
//code for attacking
}
void attack(String bodyPart, int Damage){
//code for specific attack
}
如果 Apprentice 类有 2 个名称完全相同且参数完全相同的方法,那么 Master 类中定义的攻击方法是否会被重载或覆盖?
不会同时被覆盖和重载吗!?
【问题讨论】:
-
搜索提出的问题。读。 仅如果还有其他问题,请提出。
-
会超载
-
attack方法在Master类中被重载,每个重载的方法都将被Apprentice类中的方法覆盖。 -
Apprentice.attack(String)将覆盖Master.attack(String)。Apprentice.attack(String, int)将覆盖Master.attack(String, int)。attack(String)和attack(String, int)是重载。 -
@RamandeepSinghBedi 因为您没有正确研究。这是一个简单的问题,之前在 SO 上也有人问过。
标签: java methods polymorphism overloading overriding