请问如何用Java编写一个汽车类Car
String color;//车的颜色
int door;//车门数量
float speed;//车的速度
编写一个无参构造方法,构造对象的同时给各属性赋值。
public Car(){
//请填写相应的代码
}
编写一个具有3个参数的构造方法
public Car(String color,int door,float speed){
//请填写相应的代码
}
public void start(){
//汽车启动。输出汽车已启动,并输出汽车的各个属性
}
public void speedUp(float speed){
//加速
}
public void shutDown(float speed){
//减速
}
public void brake(){
//刹车
}
编写一个类Test,测试上面的汽车类。 展开
public class Car {
private String color;//颜色
private int door;//车门数量
private float speed;//车速
public Car(){
this.color = "红色";
this.door = 3;
this.speed = 110;
}
public Car(String color, int door, float speed) {
this.color = color;
this.door = door;
this.speed = speed;
}
public void start(){
//汽车启动。输出汽车已启动,并输出汽车的各个属性
System.out.println("汽车已启动,汽车颜色为"+color+",车门数为"+door+",车速为"+speed);
}
public void speedUp(float speed){
//加速
System.out.println("汽车加速到"+speed+"km/h");
}
public void shutDown(float speed){
//减速
System.out.println("汽车减速到"+speed+"km/h");
}
public void brake(){
//刹车
System.out.println("已刹车");
}
}
public class Test {
public static void main(String[] args){
Car car = new Car();
car.start();
car.speedUp(100);
car.shutDown(60);
car.brake();
Car car1 = new Car("白色",4,20.2F);
car1.start();
car1.speedUp(100);
car1.shutDown(60);
car1.brake();
}
}
运行结果
public class CarInfo{
private String color;
private int door;
private float speed;
CarInfo(){
this.color="red";
this.door=4;
this.speed=80F;
}
public void setColor(String color){
this.color=color;
}
public String getColor(){
return this.color;
}
public void setDoor(int door){
this.door=door;
}
public int getDoor(){
return this.door;
}
public void setSpeed(float speed){
this.speed=speed;
}
public float getSpeed(){
return this.speed;
}
}
// 请填写相应的代码
String color;// 车的颜色
int door;// 车门数量
float speed;// 车的速度
// 编写一个无参构造方法,构造对象的同时给各属性赋值。
public Car() {
// 请填写相应的代码
color = "black";
door = 4;
speed = 120.0f;
}
// 编写一个具有3个参数的构造方法
public Car(String color, int door, float speed) {
this.color = color;
this.door = door;
this.speed = speed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getDoor() {
return door;
}
public void setDoor(int door) {
this.door = door;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
public void start() {
// 汽车启动。输出汽车已启动,并输出汽车的各个属性
System.out.println("汽车已启动...");
System.out.println("汽车的颜色:"+color+",汽车的门:"+door+",汽车的速度:"+speed);
}
public void speedUp(float speed) {
// 加速
System.out.println("汽车正在加速。。。");
}
public void shutDown(float speed) {
// 减速
System.out.println("汽车正在减速。。。");
}
public void brake() {
// 刹车
System.out.println("汽车正在刹车。。。");
}
public static void main(String[] args) {
Car car = new Car();
car.speedUp(car.getSpeed());
car.shutDown(car.getSpeed());
car.brake();
}
}
2019-04-26
this.color='黑';
this.door=4;
this.speed=100;
}
编写一个具有3个参数的构造方法
public Car(String color,int door,float speed){
this.color=color;
this.door=door;
this.speed=speed;
}
public void start(){
//汽车启动。输出汽车已启动,并输出汽车的各个属性
System.out.println("汽车启动");
System.out.println("颜色:"+this.color+"车门数量:"+this.door+"速度"+this,speed);
}
public void speedUp(float speed){
//加速
//传入的为增加的速度
this.speed=this.speed+speed;
//传入的为增加后的速度
this.speed=speed;
}
public void shutDown(float speed){
//减速
//传入的为的速度
this.speed=this.speed-speed;
//传入的为增加后的速度
this.speed=speed;
}
public void brake(){
//刹车
this.speed=0;
}