继承上机作业

package bbb;
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()");
}
}

public class music {
public static void tune(Instrument i){
i.play();
}
public static void main(String[]args){
music i=new music();
Wind s=new Wind();
Brass c=new Brass();
Instrument e=new Instrument();
i.tune(e);
i.tune(s);
i.tune(c);

}

}

/4、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数
loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。
/

package bbb;
import java.util.*;
class vehicle{
int wheels;
int weight;
public vehicle() {
Scanner a=new Scanner(System.in);
Scanner b=new Scanner(System.in);
System.out.println("请输入车轮个数:");
this.wheels=a.nextInt();
System.out.println("请输入车重:");
this.weight=b.nextInt();
}
public void print() {
System.out.println("轮胎个数:"+wheels);
System.out.println("车重:"+weight);
}
}

class car extends vehicle{
int loader;
public car() {
super();
Scanner a=new Scanner(System.in);
System.out.println("请输入有载人数:");
this.loader=a.nextInt();
}

public void printf() {
	System.out.println("载人数:"+loader);
}

}

class truct extends car{
int payload;
public truct() {
super();
Scanner a=new Scanner(System.in);
System.out.println("请输入有载重量:");
this.payload=a.nextInt();
}
public void println() {
System.out.println("有载重量:"+payload);
}
}

public class h{
public static void main(String[]args) {
truct s3=new truct();
//s3.print();
//s3.printf();
s3.println(); //s3可用car类,vehicle类的原因是truck是他们的子类

}

}

/编写一个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 bbb;
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("别说话!认真思考!");
}

}

public class E {
public static void main(String[]args){
Monkey s1=new Monkey(" ");
s1.speak();
People s2=new People(" ");
s2.speak();
s2.think();
}
}

/3、按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,
和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、
宽、高,输出其底面积和体积。
/
package bbb;
import java.util.
;
class rectangle{
int length,width;
public void count(int length,int width){
int s;
s=length
width;
System.out.println("面积为:"+s);
}
}
class cuboid extends rectangle{
int high;
public void bulk(int length,int width,int high){
int v;
v=highwidthlength;
System.out.println("体积为:"+v);
}
public static int nextInt() {
// TODO 自动生成的方法存根
return 0;
}

}
public class Circle {
public static void main(String[]args){
Scanner a=new Scanner(System.in);
Scanner b=new Scanner(System.in);
Scanner c=new Scanner(System.in);
System.out.println("请输入长:");
int length=a.nextInt();
System.out.println("请输入宽:");
int high=b.nextInt();
System.out.println("请输入高:");
int width=c.nextInt();
rectangle s1=new rectangle();
cuboid s2=new cuboid();
s1.count(length,width);
s2.bulk(length,width,high);
}

}

原文地址:https://www.cnblogs.com/lusilin/p/10812250.html