定义card类 java_CarD.java

package bdqn;

//汽车类

/*定义汽车类(Car),有品牌(brand)、座位数(seatCount)、排量(displacement),油量(oilAmount)等属性。

有启动(start)、行驶(run)、刹车(brake)、加速(speedUp)、停止(stop)等方法,

汽车在启动时,行驶中油量会逐步减少,当油量是0时汽车无法启动、行驶。用面向对象的思想模拟这个过程。(测试数据信息自定)。*/

public class CarD {

String brand;

int seatCount;

String displacement;

int oilAmount;

// 显示车辆信息

public CarD(String brand, int seatCount, String displacement, int oilAmount) {

this.brand = brand;

this.seatCount = seatCount;

this.displacement = displacement;

this.oilAmount = oilAmount;

System.out.println("品牌:" + this.brand + "座位数:" + this.seatCount + ",排量为:" + this.displacement + ",油量:"

+ this.oilAmount + "升。");

}

// 启动方法

public void start() {

System.out.println("这辆" + this.brand + "在启动");

}

// 行驶方法

public void run(int oilAmount) {

if (oilAmount == 0) {

System.out.println("当油量为0时,这辆" + this.brand + "牌轿车无法行驶!");

}

}

// 刹车方法

public void brake() {

System.out.println("这辆" + this.brand + "牌轿车的刹车性能良好");

}

// 加速方法

public void speedUp() {

System.out.println("这辆" + this.brand + "牌轿车正在加速!");

}

// 停止方法

public void stop() {

System.out.println("这辆" + this.brand + "牌轿车停车熄火了!");

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

package bdqn; //汽车类 /*定义汽车类(Car),有品牌(brand)、座位数(seatCount)、排量(displacement),油量(oilAmount)等属性。 有启动(start)、行驶(run)、刹车(brake)、加速(speedUp)、停止(stop)等方法, 汽车在启动时,行驶中油量会逐步减少,当油量是0时汽车无法启动、行驶。用面向对象的思想模拟这个过程。(测试数据信息自定)。*/ public class CarD { String brand; int seatCount; String displacement; int oilAmount; // 显示车辆信息 public CarD(String brand, int seatCount, String displacement, int oilAmount) { this.brand = brand; this.seatCount = seatCount; this.displacement = displacement; this.oilAmount = oilAmount; System.out.println("品牌:" + this.brand + "座位数:" + this.seatCount + ",排量为:" + this.displacement + ",油量:" + this.oilAmount + "升。"); } // 启动方法 public void start() { System.out.println("这辆" + this.brand + "在启动"); } // 行驶方法 public void run(int oilAmount) { if (oilAmount == 0) { System.out.println("当油量为0时,这辆" + this.brand + "牌轿车无法行驶!"); } } // 刹车方法 public void brake() { System.out.println("这辆" + this.brand + "牌轿车的刹车性能良好"); } // 加速方法 public void speedUp() { System.out.println("这辆" + this.brand + "牌轿车正在加速!"); } // 停止方法 public void stop() { System.out.println("这辆" + this.brand + "牌轿车停车熄火了!"); } } 一键复制 编辑 Web IDE 原始数据 按行查看 历史
经验分享 程序员 微信小程序 职场和发展