您的位置:首页 > 编程语言 > Java开发

JAVA 重写

2015-10-29 15:51 337 查看
/**
* 目的:重写
*/
package com.sunp.mye;

class Father{
public void Add()
{
System.out.println("A add...");
}
public void Addall() {
System.out.println("A add all..");
Add();//由于子类中重写了父类的Add,所以调用了子类的Add
this.Add();//同上。同时this的实例为Child类,因此它的Add也是子类的Add
}
}
class Child extends Father{
public void Add()
{
System.out.println("B add...");
}
public void Addall() {
System.out.println("B add all..");
super.Addall();
}
}
public class MyThis {
public static void main(String[] args) {
// TODO Auto-generated method stub
Child b = new Child();
b.Addall();
}

}

输出结果:

B add all..

A add all..

B add...

B add...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: