class Person{public int c;private String name;private int age;protected void setName(String name){this.name=name;}protected void setAge(int age){this.age=age;}protected void print(){System.out.println("Name="+name+" Age="+age);}}public class DemoSuper extends Person{public void print(){System.out.println("DemoSuper:");super.print();}public static void main(String[] args){DemoSuper ds=new DemoSuper();ds.setName("kevin");ds.setAge(22);ds.print();}}
在DemoSuper中,重新定义的print方法覆写了父类的print方法,它首先做一些自己的事情,然后调用父类的那个被覆写了的方法。输出结果说明了这一点:
(编辑:aniston)
·2024年12月目录 ·2024年11月目录 ·2024年10月目录 ·2024年9月目录 ·2024年8月目录 ·2024年7月目录 ·2024年6月目录 ·2024年5月目录 ·2024年4月目录 ·2024年3月目录 ·2024年2月目录 ·2024年1月目录 ·2023年12月目录 ·2023年11月目录