重写(Override)和重载(Overlord)详解
一.重写(Override)
package AbstractTest.OverrideOverlord;
public class Animal {
public void move() {
System.out.println("动物可以移动");
}
}
package AbstractTest.OverrideOverlord;
public class Dog extends Animal{
//重写了父类的方法move 重写发生在父类的子类之间
@Override
public void move() {
System.out.println("狗可以跑和走");
}
}
package AbstractTest.OverrideOverlord;
public class Test {
public static void main(String[] args) {
Animal a=new Animal();//Animal对象
Dog d=new Dog();//Dog对象
a.move();
d.move();
}
}
"C:\Program Files (x86)\Java\jdk1.8.0_172\bin\java.exe" -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=49629:D:\ideaIU-2018.1.5.win\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\rt.jar;D:\TestDemo\out\production\TestDemo" AbstractTest.OverrideOverlord.Test
动物可以移动
狗可以跑和走
Process finished with exit code 0
在上面的例子中可以看到,尽管b属于Animal类型,但是它运行的是Dog类的move方法。 这是由于在编译阶段,只是检查参数的引用类型。 然而在运行时,Java虚拟机(JVM)指定对象的类型并且运行该对象的方法。 因此在上面的例子中,之所以能编译成功,是因为Animal类中存在move方法,然而运行时,运行的是特定对象的方法。
方法的重写规则
参数列表必须完全与被重写方法的相同; 返回类型与被重写方法的返回类型可以不相同,但是必须是父类返回值的派生类(java5 及 更早版本返回类型要一样,java7 及更高版本可以不同);
注意:
二.重载(Overload)
package AbstractTest.OverrideOverlord;
public class Cat {
public void move(){
System.out.println("猫可以移动");
}
public void move(String name){
System.out.println(name+"可以抓老鼠");
}
}
package AbstractTest.OverrideOverlord;
public class Test {
public static void main(String[] args) {
Cat c=new Cat();
c.move();
c.move("小花");
}
}
"C:\Program Files (x86)\Java\jdk1.8.0_172\bin\java.exe" -javaagent:D:\ideaIU-2018.1.5.win\lib\idea_rt.jar=49845:D:\ideaIU-2018.1.5.win\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_172\jre\lib\rt.jar;D:\TestDemo\out\production\TestDemo" AbstractTest.OverrideOverlord.Test
猫可以移动
小花可以抓老鼠
Process finished with exit code 0
重载规则:
总结