问题:
设计一个车辆类Vehicle,该Vehicle类具有属性:
车辆型号(vehId),用途(vehUse),性能(author),油耗(oil),颜色(color),价钱(price)。
要求属性私有,生成其共有的get/set 方法.
具有方法:show():该方法内显示该车的的详细信息
具有方法:start(),stop();
在创建一个Vehicle对象的同时给该对象各属性赋初值。(带参构造)
编写测试类TestVehicle,实现创建一个Vehicle对象Vehicle,打印出该车的详细信息。
Vehicle.java
public class Vehicle {
public Vehicle(String vehId, String vehUse, String author, double oil, String color, double price) {
this.vehId = vehId;
this.vehUse = vehUse;
this.author = author;
this.oil = oil;
this.color = color;
this.price = price;
}
private String vehId;
private String vehUse;
private String author;
private double oil;
private String color;
private double price;
public void setAuthor(String author) {
this.author = author;
}
public void setColor(String color) {
this.color = color;
}
public void setOil(double oil) {
this.oil = oil;
}
public void setPrice(double price) {
this.price = price;
}
public void setVehId(String vehId) {
this.vehId = vehId;
}
public void setVehUse(String vehUse) {
this.vehUse = vehUse;
}
public String getAuthor() {
return author;
}
public String getColor() {
return color;
}
public double getOil() {
return oil;
}
public double getPrice() {
return price;
}
public String getVehId() {
return vehId;
}
public String getVehUse() {
return vehUse;
}
void show(){
System.out.println("the vehId is: "+vehId);
System.out.println("the vehUse is: "+vehUse);
System.out.println("the author is: "+author);
System.out.println("the oil is: "+oil);
System.out.println("the color is: "+color);
System.out.println("the price is: "+price);
}
void start(){
System.out.println("the car is going...");
}
void stop(){
System.out.println("the car is stop...");
}
}
Test.java
public class Test {
public static void main(String[]args){
Vehicle vehicle=new Vehicle("tasla","travel","good",0,"black",10000);
vehicle.show();
vehicle.start();
vehicle.stop();;
}
}
运行结果: