继承上机作业

1、实现如下类之间的继承关系,并编写Music类来测试这些类。

package bbb;
public 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");
 }
 }
 
package bbb;
public class music {
 public static void tune(instrument i){
  i.play();
 }
  public static void main(String[] args) {
   instrument c=new  instrument();
   wind a=new  wind();
   brass b=new brass();
   tune(c);
   tune(a);
   tune(b);
  }
 }
 

2、编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类E。要求:

(1) Monkey类中有个构造方法:Monkey (String s),并且有个跑了方法,在speak方法中输出“咿咿呀呀......”的信息。

(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。

(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。

(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。

package bbb;
public 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("小样的,不错啊,会说话了");
 }
 public void think(){
  System.out.println("别说话,认真思考");
 }
}
 
package bbb;
public class E {
  public static void main(String[] args) {
   monkey a=new monkey("。。。。。");
   people b=new people("。。。。。");
   a.speak();
   b.speak();
   b.think();
  }
}

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

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

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

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

 

package ccc;

class tt {

int l=1;
int w=2;
int s;
int cs(int l,int w){
s=l*w;
return s;
}
public void play(){
System.out.println("矩阵的面积:"+s);
}
}
class Cuboid extends tt {
int v;
int cs(int l,int w,int h){
v=l*w*h;
return v;
}
public void play(){
System.out.println("长方体的体积:"+v);
}
}
public class rectangular{
public static void tune(tt i){
i.play();
}
public static void main(String[] args){
rectangular o=new rectangular();
tt a=new tt();
Cuboid c=new Cuboid();
o.tune(a);
o.tune(c);
}

}

posted @ 2019-05-12 09:15  阿铭。  阅读(103)  评论(0编辑  收藏  举报