用笨办法写的作业 有老师或大大看看达标不?
我用笨办法写的 写了5个类
一个父类 五个子类 一个main类 想知道如何能优化 更省代码省空间呢?
如下:
父类 Transp
public class Transp {
public String transpName;
public String transpStyle;
public int transpNum;
public String getTranspName() {
return transpName;
}
public void setTranspName(String transpName) {
this.transpName = transpName;
}
public String getTranspStyle() {
return transpStyle;
}
public void setTranspStyle(String transpStyle) {
this.transpStyle = transpStyle;
}
public int getTranspNum() {
return transpNum;
}
public void setTranspNum(int transpNum) {
this.transpNum = transpNum;
}
public void setFields() {
setTranspName(null);
setTranspStyle(null);
setTranspNum(0);
}
}
子类 Bus
public class Bus extends Transp {
public void setFields() {
setTranspName("Bus");
setTranspStyle("land");
setTranspNum(40);
}
}
子类 Plane
public class Plane extends Transp {
public void setFields() {
setTranspName("Plane");
setTranspStyle("air");
setTranspNum(100);
}
}
子类 Ship
public class Ship extends Transp {
public void setFields() {
setTranspName("Ship");
setTranspStyle("sea");
setTranspNum(200);
}
}
子类 SpacePlane
public class SpacePlane extends Transp {
public void setFields() {
setTranspName("SpacePlane");
setTranspStyle("spcace");
setTranspNum(20);
}
}
子类 Train
public class Train extends Transp {
public void setFields() {
setTranspName("Train");
setTranspStyle("rail");
setTranspNum(500);
}
}
main类
import java.util.Scanner;
public class mainTransp {
public static void main(String[] args) {
double scannerNum;
Scanner input = new Scanner(System.in);
for (int i = 0; i < 6; i++) {
System.out.println("请输入查询序号: 1为Bus 2为Plane 3为Ship 4为SpacePlane 5为Train 0为查询所有");
scannerNum = input.nextDouble();
if (scannerNum < 0 || scannerNum > 5 || scannerNum != (int) scannerNum) {
System.out.println("输入有误 请重新输入!");
continue;
} else {
if (scannerNum == 1 || scannerNum == 0) {
Bus set = new Bus();
set.setFields();
System.out.println("运输方式:" + set.getTranspName() + "\n运输路线为:" + set.getTranspStyle() + "\n运送人数为:"
+ set.getTranspNum() + "\n");
if (scannerNum != 0) {
break;
}
}
if (scannerNum == 2 || scannerNum == 0) {
Plane set = new Plane();
set.setFields();
System.out.println("运输方式:" + set.getTranspName() + "\n运输路线为:" + set.getTranspStyle() + "\n运送人数为:"
+ set.getTranspNum() + "\n");
if (scannerNum != 0) {
break;
}
}
if (scannerNum == 3 || scannerNum == 0) {
Ship set = new Ship();
set.setFields();
System.out.println("运输方式:" + set.getTranspName() + "\n运输路线为:" + set.getTranspStyle() + "\n运送人数为:"
+ set.getTranspNum() + "\n");
if (scannerNum != 0) {
break;
}
}
if (scannerNum == 4 || scannerNum == 0) {
SpacePlane set = new SpacePlane();
set.setFields();
System.out.println("运输方式:" + set.getTranspName() + "\n运输路线为:" + set.getTranspStyle() + "\n运送人数为:"
+ set.getTranspNum() + "\n");
if (scannerNum != 0) {
break;
}
}
if (scannerNum == 5 || scannerNum == 0) {
Train set = new Train();
set.setFields();
System.out.println("运输方式:" + set.getTranspName() + "\n运输路线为:" + set.getTranspStyle() + "\n运送人数为:"
+ set.getTranspNum() + "\n");
if (scannerNum != 0) {
break;
}
}
break;
}
}
}
}
运行后需要获取输入数字 是0-5的整数则相应的输出对应的交通工具和运送人数 0为输出所有 错误数字有提示
输入0 运行如下:
请输入查询序号: 1为Bus 2为Plane 3为Ship 4为SpacePlane 5为Train 0为查询所有
0
运输方式:Bus
运输路线为:land
运送人数为:40
运输方式:Plane
运输路线为:air
运送人数为:100
运输方式:Ship
运输路线为:sea
运送人数为:200
运输方式:SpacePlane
运输路线为:spcace
运送人数为:20
运输方式:Train
运输路线为:rail
运送人数为:500
有老师指点一下吗 谢谢