5.2.1
程序模板
继承
按模板要求,将【代码 1】~【代码 12】替换为 Java 程序代码。
Example.java class People { protected double weight,height; public void speakHello( ) { System.out.println("yayawawa"); } public void averageHeight() { height=173; System.out.println("average height:"+height); } public void averageWeight( ) { weight=70; System.out.println("average weight:"+weight); } } class ChinaPeople extends People { public void speakHello() { System.out.println("你好, 吃了吗");// 代码 1】 // 重写 public void speakHello( ) 【 方法,要求输出类似“你好,吃了吗” 这样的汉语信息 System.out.println("中国人的平均身高: 168.78 厘米");// 【代码 2】 // 重写 public void averageHeight( )方法,要求输出类似“中国人的平均身高:168.78 厘米”这样的 汉语信息 System.out.println("中国人的平均体重: 公斤");// 65 【代码 3】// 重写 public void averageWeight( )方法,要求输出类似“中国人的平均体重:65 公斤”这样的汉语信 息 }
public void chinaGongfu () { System.out.println("坐如钟,站如松,睡如弓");//【代码 4】// 输出中国武术 的信息,如"坐如钟,站如松,睡如弓"等 } } class AmericanPeople extends People { public void speakHello() { System.out.println("How do you do ");// 代码 5】 //重写 public void speakHello() 【 方法,要求输出类似“How do you do”这样的英语信息 } public void averageHeight() { height=173; System.out.println("average height:"+height); //【代码 6】 // 重写 public void averageHeight( )方法 } public void averageWeight( ) { weight=70; System.out.println("average weight:"+weight);// 【代码 7】 // 重写 public void averageWeight( )方法 } public void americanBoxing() { System.out.println("直拳、钩拳");//【代码 8】 // 输出拳击的信息,如 “直拳”“钩拳”等 、 } } class BeijingPeople extends ChinaPeople { public void speakHello() { System.out.println("您好");//【代码 9】 // 重写 public void speakHello( )方法, 要求输出类似“您好”这样的汉语信息 } public void averageHeight() { height=173; System.out.println("average height:"+height); // 【代码 10】 // 重写 public void averageHeight( )方法
} public void averageWeight( ) { weight=70; System.out.println("average weight:"+weight);//【代码 11】 // 重写 public void averageWeight( )方法 } public void beijingOpera() { System.out.println("我爱京剧");//【代码 12】 // 输出京剧的信息 } } public class Example { public static void main(String args[ ]) { ChinaPeople chinaPeople=new ChinaPeople( ); AmericanPeople americanPeople=new AmericanPeople( ); BeijingPeople beijingPeople=new BeijingPeople( ); chinaPeople.speakHello( ); americanPeople.speakHello( ); beijingPeople.speakHello( ); chinaPeople.averageHeight( ); americanPeople.averageHeight( ); beijingPeople.averageHeight( ); chinaPeople.averageWeight( ); americanPeop
le.averageWeight( ); beijingPeople.averageWeight( ); chinaPeople.chinaGongfu( ); americanPeople.americanBoxing( ); beijingPeople.beijingOpera( ) ; beijingPeople.chinaGongfu( ); } }
5.2.2 上转型对象 程序模板 按模板要求,将【代码 1】~【代码 4】替换为 Java 程序代码。 HardWork.java abstract class Employee { public abstract double earnings( ); } class YearWorker extends Employee { public double earnings( ) {return 2.345}//【代码 1】 // 重写 earnings( )方法 } class MonthWorker extends Employee { public double earnings( ) {return 1.265}//【代