public class TextJiCheng { public static void main(String[] args) { // TODO 自动生成的方法存根 Father f=new Father(); f.setName("父亲"); f.setAge(50); System.out.println("名字是:"+f.getName()+"年龄是:"+f.getAge()); f.work(); System.out.println(); Son s=new Son(); s.setName("儿子"); s.setAge(20); System.out.println("名字是:"+s.getName()+"年龄是:"+s.getAge()); s.work(); s.sing(); } }
public class Son extends Father { //Object a;所有类的父类 public Son() { System.out.println("子类的构造方法"); } public void sing() { System.out.println("我喜欢唱歌"); } //覆盖(重写)只有继承中才出现 public void work() { System.out.println("我不喜欢上班,我要去唱歌"); } }
运行输出:
父类的构造方法
名字是:父亲年龄是:50
我劳动我光荣
父类的构造方法
子类的构造方法
名字是:儿子年龄是:20
我不喜欢上班,我要去唱歌
我喜欢唱歌
原文:http://www.cnblogs.com/panyiquan/p/5252327.html