继承-people
创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople
和AmericanPeople类重写父类的三个方法)。
1 package com.hanqi.z1p1; 2 3 public class People { 4 5 protected double height; 6 protected double weight; 7 8 public void speakHello() 9 { 10 System.out.println("Hello!"); 11 } 12 public void averageHeight() 13 { 14 System.out.println("平均身高"); 15 } 16 public void averageWeight() 17 { 18 System.out.println("平均体重"); 19 } 20 21 }
1 package com.hanqi.z1p1; 2 3 public class ChinaPeople extends People { 4 5 public void chinaGongfu() 6 { 7 System.out.println("坐如钟,站如松,睡如弓"); 8 } 9 10 public void speakHello() 11 { 12 System.out.println("你好"); 13 } 14 public void averageHeight() 15 { 16 System.out.println("中国平均身高为170"); 17 } 18 public void averageWeight() 19 { 20 System.out.println("中国平均体重为140"); 21 } 22 }
1 package com.hanqi.z1p1; 2 3 public class AmericanPeople extends People{ 4 5 public void americanBoxing() 6 { 7 System.out.println("直拳,勾拳"); 8 } 9 10 public void speakHello() 11 { 12 System.out.println("Hi"); 13 } 14 public void averageHeight() 15 { 16 System.out.println("美国平均身高为170"); 17 } 18 public void averageWeight() 19 { 20 System.out.println("美国平均体重为140"); 21 } 22 23 24 25 }
package com.hanqi.z1p1; public class TestPeople0522 { public static void main(String[] args) { // TODO 自动生成的方法存根 People p=new People(); p.speakHello(); p.averageHeight(); p.averageWeight(); ChinaPeople cp=new ChinaPeople(); cp.speakHello(); cp.averageHeight(); cp.averageWeight(); AmericanPeople ap=new AmericanPeople(); ap.speakHello(); ap.averageHeight(); ap.averageWeight(); } }
运行结果: