[20-05-02][Self-test 33]Java Car

Posted mirai3usi9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[20-05-02][Self-test 33]Java Car相关的知识,希望对你有一定的参考价值。

 1 package test_7_3;
 2 
 3 public class Car {
 4 
 5     /**
 6      * 类的成员变量 请定义一个交通工具的类其中有: 
 7      * 属性速度和类型,方法移动,设置速度,加速,减速等。最后测试
 8      */
 9     
10     public int speed;
11     public String tpye;
12     
13     public void move(boolean move) {
14         
15         if (move == true) {
16             speedUp();
17         } else {
18             speedDown();
19         }
20     }
21     
22     public void speedUp() {
23         
24         this.speed += 50;
25         
26         System.out.println("当前速度为:" + this.speed);
27     }
28     
29     public void speedDown() {
30         
31         if (speed >= 50) {
32             this.speed -= 50;
33         } else {
34             this.speed = 0;
35         }
36         
37         System.out.println("当前速度为:" + this.speed);
38         
39     }
40     
41     public void setSpeed(int speed) {
42         
43         this.speed = speed;
44         System.out.println("当前速度为:" + this.speed);
45     }
46     
47 }

 

 1 package test_7_3;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6 
 7         Car car = new Car();
 8         
 9         car.setSpeed(130);
10         
11         car.speedUp();
12         car.speedDown();
13         car.speedDown();
14         car.speedDown();
15         car.speedDown();
16     }
17 
18 }

 

结果如下:

当前速度为:130
当前速度为:180
当前速度为:130
当前速度为:80
当前速度为:30
当前速度为:0

以上是关于[20-05-02][Self-test 33]Java Car的主要内容,如果未能解决你的问题,请参考以下文章

[20-05-02][Self-test 32]Java GuessNum

[20-05-02][Self-test 35]Java OverRide

[20-04-26][Self-test 6]Java CharType

[20-05-09][Self-test 40]Java BankSystem 1

[20-05-09][Self-test 40]Java BankSystem 1

[20-06-04][Self-test 46]Java Linker Manage