2016-06-29
Java只支持单根继承关系。
package com.java1995; /** * 父类:人类 * @author Administrator * */ public class People { /** * 封装 */ private String name; private int age; /** * 封装的实现 */ public void eat(){ System.out.println("人在吃饭"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.java1995; /** * 二级子类:中国人 * @author Administrator * */ public class Chinese extends People{ /** * 继承 */ public Chinese(){ super(); } /** * 子类方法 */ public void speakChinese(){ System.out.println("中国人说汉语"); } }
package com.java1995; /** * 三级子类:河北人 * @author Administrator * */ public class HebeiPeople extends Chinese{ /** * 子类方法 */ public void speakHebeiLang(){ System.out.println("河北人说河北话"); } }
package com.java1995; /** * 测试类 * @author Administrator * */ public class Test { public static void main(String[] args) { HebeiPeople h=new HebeiPeople(); h.speakHebeiLang(); } }
【参考资料】