java定义一个交通工具类 - 爱问答

(爱问答)

java定义一个交通工具类

定义一个交通工具类,其中有属性:速度(doulbe类型)、乘客数(静态、整型),具有方法:移动、设置速度、加速和减速。具有一个带参的构造方法,能够给乘客数赋值。 创建一个测试类,在其主方法main中创建两个交通工具类的对象,分别设置速度和乘客数。调用各自的加速或减速功能修改速度值,并且分别打印这两个对象的速度和乘客数值。

public class Transport {

   public double speed;

   public static Integer number;

   public Transport(Integer number) {

       Transport.number = number;

   }

   public void accelerate(double s) {

       speed = speed + s;

   }

   public void dccelerate(double s) {

       speed = speed - s;

   }   

public void setSpeed(double speed) {

       this.speed = speed;    }

}


class Test{

   public static void main(String[] args) {

       Transport transport1 = new Transport(10);

       transport1.setSpeed(30);

       transport1.accelerate(10);

       System.out.println("transport1的速度:"+transport1.speed+",乘客:"+transport1.number);

       Transport transport2 = new Transport(10);

       transport2.setSpeed(4);

       transport2.accelerate(5);

       System.out.println("transport1的速度:"+transport2.speed+",乘客:"+transport2.number);

   }

}

相关标签:java

下一篇:OracleVMVirtualBox安装以后打不开

上一篇:前端编程如何快速入门

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?