编写一个Java程序,能够处理房屋、住宅、写字楼的信息。
分析楼房、住宅、写字楼之间的关系,分别定义类。其中,楼房是基类,用于描述一座楼房的层数、总平方米数。住宅除了具有楼房的属性外,还有卧室数、浴室数;写字楼除了具有楼房的属性外,还有房间数和灭火器的数量。
编程定义类,在主程序中定义住宅类的对象和写字楼类的对象,设置信息并输出对象的信息。
系统参考代码如下:
/* 请在下面的【】处补充代码
*/
//测试类
public class Main {
public static void main(String[] args) {
Building build = new Building();
build.setBuilding(5, 5000);
build.show();
House house = new House();
house.setHouse(6,6000,120,60);
house.show();
OfficeBuilding office = new OfficeBuilding();
office.setOffice(10, 15000, 50, 200);
office.show();
}
}
//楼房类:描述一座楼房的层数、总平方米数
class Building {
int layers; //层数
int area; //面积(平方米数)
//设置楼房信息
public void setBuilding(int layers, int area) {
【 】
}
//显示楼房信息
public void show() {
System.out.printf("层数:%d,总面积:%d",layers,area);
}
}
//住宅除了具有楼房的属性(层数、总面积)外,还有卧室数、浴室数
class House 【】{
int bedRooms;
int bathRooms;
public void setHouse(int layers, int area, int bedRooms,int bathRooms) {
【】
}
@Override
public void show() {
System.out.print("\n住宅:");
【】
}
}
//写字楼除了具有楼房的属性(层数、面积)外,还有房间数和灭火器的数量
class OfficeBuilding extends Building {
int rooms; //房间数
int extinguishers; //灭火器数量
public void setOffice(int layers, int area, int rooms,int extinguishers) {
【】
}
@Override
public void show() {
System.out.print("\n写字楼:");
【】
}
}
输入格式:
无。
输出格式:
层数:xx,总面积:xxxx
住宅:层数:x,总面积:xxxx卧室数:xxxx,浴室数:xx
写字楼:层数:xx,总面积:xxxxx房间数:xx,灭火器数:xxx
输入样例:
输出样例:
层数:5,总面积:5000
住宅:层数:6,总面积:6000卧室数:120,浴室数:60
写字楼:层数:10,总面积:15000房间数:50,灭火器数:200
public class Main {
public static void main(String[] args) {
Building build = new Building();
build.setBuilding(5, 5000);
build.show();
House house = new House();
house.setHouse(6,6000,120,60);
house.show();
OfficeBuilding office = new OfficeBuilding();
office.setOffice(10, 15000, 50, 200);
office.show();
}
}
//楼房类:描述一座楼房的层数、总平方米数
class Building {
int layers; //层数
int area; //面积(平方米数)
//设置楼房信息
public void setBuilding(int layers, int area) {
this.layers=layers;
this.area=area;
}
//显示楼房信息
public void show() {
System.out.printf("层数:%d,总面积:%d",layers,area);
}
}
//住宅除了具有楼房的属性(层数、总面积)外,还有卧室数、浴室数
class House extends Building{
int bedRooms;
int bathRooms;
public void setHouse(int layers, int area, int bedRooms,int bathRooms) {
this.layers=layers;
this.area=area;
this.bedRooms=bedRooms;
this.bathRooms=bathRooms;
}
@Override
public void show() {
System.out.print("\n住宅:");
System.out.printf("层数:%d,总面积:%d卧室数:%d,浴室数:%d",layers,area,bedRooms,bathRooms);
}
}
//写字楼除了具有楼房的属性(层数、面积)外,还有房间数和灭火器的数量
class OfficeBuilding extends Building {
int rooms; //房间数
int extinguishers; //灭火器数量
public void setOffice(int layers, int area, int rooms,int extinguishers) {
this.layers=layers;
this.area=area;
this.rooms=rooms;
this.extinguishers=extinguishers;
}
@Override
public void show() {
System.out.print("\n写字楼:");
System.out.printf("层数:%d,总面积:%d房间数:%d,灭火器数:%d",layers,area,rooms,extinguishers);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)