求java大神啊

题目:定义一个交通工具(Vehicle)的类,其中有属性:交通工具名称(name)体积(size)、重量((weight、当前速度((speed)方法:构造方法(设置名称... 题目:定义一个交通工具(Vehicle)的类,其中有属性:交通工具名称(name)体积(size)、重量((weight、当前速度((speed)方法:构造方法(设置名称、体积、重量、初始速度(0)),加速speedup,减速speeddown0,打印体积、重量、当前速度等状态数据的方法。在测试类的main0方法中实例化一个交通工具对象,通过构造方法给它的体积、重量、初始速度赋初值,并且打印出来。然后,对此对象进行加速、减速操作,并输出其状态数据。 展开
 我来答
yinfengnong
2019-06-12 · TA获得超过5619个赞
知道大有可为答主
回答量:2344
采纳率:89%
帮助的人:2276万
展开全部

代码如下:

class Vehicle {

private String name;

private int size;

private int weight;

private int speed;

public Vehicle(String name, int size, int weight) {

this(name, size, weight, 0);

}

public Vehicle(String name, int size, int weight, int speed) {

this.name = name;

this.size = size;

this.weight = weight;

this.speed = speed;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getSize() {

return size;

}

public void setSize(int size) {

this.size = size;

}

public int getWeight() {

return weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

public int getSpeed() {

return speed;

}

public void setSpeed(int speed) {

this.speed = speed;

}

public void speedUp(int speed) {

this.speed += speed;

}

public void speedDown(int speed) {

this.speed -= speed;

if (this.speed < 0) {

this.speed = 0;

}

}

}

public class App {

public static void main(String[] args) {

Vehicle vehicle = new Vehicle("Car", 10, 1000, 0);

System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());

vehicle.speedUp(50);

System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());

vehicle.speedUp(50);

System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());

vehicle.speedDown(20);

System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());

}

}

运行结果:

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
来自武功湖学贯中西的海狮
2019-06-12 · TA获得超过902个赞
知道答主
回答量:3282
采纳率:12%
帮助的人:249万
展开全部
求java大神啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式