第五次上级作业

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

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("调用btass的play2");
	}
}
public class Music {
	public static void tune(Instrument i){
		i.play();
	}

	public static void main(String[] args) {
		Wind i=new Wind();
		tune(i);
		Brass j=new Brass();
		tune(j);
		

	}

}

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个类的功能。

class Monkey{
	Monkey(String s){
		System.out.println(s);
	}
	public void speak(){
	System.out.println("咿呀咿呀......");
	}
}

class People extends Monkey{

	People(String s) {
		super(s);
	}
	public void speak(){
		System.out.println("小样的,不错嘛!会说话了!");
	}
	void think(){
		System.out.println("别说话!认真思考!");
	}
	
}

public class E {

	public static void main(String[] args) {
		Monkey monkey=new Monkey("苏炎");
		monkey.speak();
		People people=new People("苏炎");
		people.speak();
		people.think();

	}

}

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

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

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

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

class Rectangle{
	double rlong;
	double rwidth;
	public double s(){
		return rlong*rwidth;
	}
}

class Cuboid extends Rectangle{
	double high;
	public double v(){
		return rlong*rwidth*high;
	}
}
public class Test {

	public static void main(String[] args) {
		Rectangle i=new Rectangle();
		i.rlong=4;
		i.rwidth=2;
		System.out.println("长方形面积为:"+i.s());
		Cuboid j=new Cuboid();
		j.rlong=i.rlong;
		j.rwidth=i.rwidth;
		j.high=5;
		System.out.println("长方体的体积为:"+j.v());

	}

}

原文地址:https://www.cnblogs.com/wjs0403/p/10812171.html