一、 继承相关
18.实现如下类之间的继承关系,并编写Music类来测试这些类。
package com.hry.test; public class Instrument { //输出弹奏乐器 public void play() { System.out.println("弹奏乐器"); } }
package com.hry.test; public class Wind extends Instrument { //输出"弹奏wind" public void play() { System.out.println("弹奏wind"); } //输出"调用wind的play2" public void play2() { System.out.println("调用wind的play2"); } }
package com.hry.test; public class Brass extends Instrument { //输出"弹奏brass" public void play() { System.out.println("弹奏brass"); } //输出"调用brass的play2" public void play2() { System.out.println("调用brass的play2"); } }
package com.hry.test; public class Music { //调用对象i的play方法 //调用tune方法,以Wind类对象为参数 ////调用tune方法,以Brass类对象为参数 public static void tune(Instrument i){ i.play(); } public static void main(String[] args) { Instrument i= new Instrument(); i.play(); Wind w= new Wind(); w.play(); w.play2(); Brass b= new Brass(); b.play(); b.play2(); } }
19.创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople
和AmericanPeople类重写父类的三个方法)。
package com.hry.test;; public class People { protected double height; protected double weight; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public void speakHello() { } public void averageHeight() { } public void averageWeight() { } }
package com.hry.test; public class ChinaPeople extends People{ public void chinaGoufu() { System.out.println("少林、太极、咏春拳"); } public void speakHello() { System.out.println("中文"); } public void averageHeight() { System.out.println("中国人平均身高为175cm"); } public void averageWeight() { System.out.println("中国人平均体重是65kg"); } }
package com.hry.test;
public class AmericnPeople extends People{ public void chinaGoufu() { System.out.println("直拳"); } public void speakHello() { System.out.println("英文"); } public void averageHeight() { System.out.println("美国人平均身高为180cm"); } public void averageWeight() { System.out.println("美国人平均体重是70kg"); } }
package com.hry.test;
public class TestPeople { public static void main(String[] args) { ChinaPeople c =new ChinaPeople(); c.speakHello(); c.averageHeight(); c.averageWeight(); AmericnPeople a=new AmericnPeople(); a.speakHello(); a.averageHeight(); a.averageWeight(); } }
21.编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类
E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()
方法,在speak方法中输出“咿咿呀呀......”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法
中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”
的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功
能。
package com.hry.test; public class Monkey { Monkey(String s) //构造 {} public void speak() { System.out.println("咿咿呀呀......"); } }
package com.hry.test; public class People1 extends Monkey{ People1(String s) { super(s); // TODO 自动生成的构造函数存根 } public void speak() { System.out.println("小样的,不错嘛!会说话了!"); } public void think() { System.out.println("别说话!认真思考!"); } }
package com.hry.test; public class E1 { public static void main(String[] args) { // TODO 自动生成的方法存根 Monkey m = new Monkey("tom"); m.speak(); People1 p = new People1("Xiaoming"); p.speak(); p.think(); } }