Java建立一个汽车(Car)类,包括品牌(String car_brand)、速度(double car_speed)两个成员变量具体看图
2个回答
展开全部
这个代码还是比较简单,下面的代码可以参考
class Car{
private String car_brand;
private double car_speed;
public Car(){ }
public Car(String brand, double speed){
this.car_brand = brand;
this.car_speed = speed;
System.out.println(this.car_brand + "汽车正以时速"
+ this.car_speed + "公里的速度行驶");
}
public void speedUp(String brand){
System.out.println(brand + "汽车正在加速行驶");
}
public void speedUp(String brand, double speed){
System.out.println(brand + "汽车正以时速"
+ speed + "公里的速度加速行驶");
}
public void speedDown(String brand){
System.out.println(brand + "汽车正在减速行驶");
}
public void speedDown(String brand, double speed){
System.out.println(brand + "汽车正以时速"
+ speed + "公里的速度减速行驶");
}
}
class Test{
public static void main(String[] args){
Car car = new Car("宝马", 100.5);
car.speedUp("法拉利");
car.speedUp("法拉利", 280.5);
car.speedDown("法拉利");
car.speedDown("法拉利", 120.9);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询