继承上机作业

1、实现如下类之间的继承关系,并编写Music类来测试这些类。
package mingye;
class Instrument { public void play(){ System.out.println("弹奏乐器!"); } } class Wind extends Instrument{ public void play(){ System.out.println("弹奏Wind!"); } public void play2(){ System.out.println("调用Wind的play2!"); } } class Brass extends Instrument{ public void play(){ System.out.println("弹奏Brass!"); } public void play2(){ System.out.println("调用Brass的play2!"); } } class Music{ public static void tune(Instrument i){ i.play(); } public static void main(String[] args){ Music i=new Music(); Wind w=new Wind(); Brass b=new Brass(); i.tune(w); i.tune(b); }}

2、编写一个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 mingye;
class Monkey { Monkey (String s){ } public void speak(){ System.out.println("咿咿呀呀!"); } } class People extends Monkey{ People(String s) { super(s); // TODO 自动生成的构造函数存根 } public void speak(){ System.out.println("小样的,不错嘛!会说话了!"); } void think(){ System.out.println("别说话!认真思考!"); } } class E{ public static void main(String[] args){ Monkey m=new Monkey(null); People p=new People(null); m.speak(); p.speak(); p.think(); } }

3、按要求编写一个Java应用程序:


(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。


(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。


(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。


 


package mingye;
class Orthogon { int l; int w; int s; int cs(int l,int w){ s=l*w; return s; } } class Cuboid extends Orthogon { int l; int w; int h; int v; int cs(int l,int w,int h){ v=l*w*h; return v; } } class Test{ public static void main(String[] args){ Orthogon o=new Orthogon(); Cuboid c=new Cuboid(); } }

 总结与心得:这次上机主要是搞清楚类与类之间的继承关系,对于构造方法这一点好像不是很理解!

posted @ 2019-05-08 22:49  明爷哥哥  阅读(127)  评论(0)    收藏  举报