From a6c31111f50251053c49726fc0d6a76bdeea6765 Mon Sep 17 00:00:00 2001 From: Luojiale <2272532647@qq.com> Date: Mon, 23 Mar 2026 10:19:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=A6=E8=BE=86=E7=AE=A1=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E4=B8=AD=E7=9A=84Car=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w1/Student.java | 0 w3/Car.java | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ w3/TestCar.java | 35 +++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 w1/Student.java create mode 100644 w3/Car.java create mode 100644 w3/TestCar.java diff --git a/w1/Student.java b/w1/Student.java new file mode 100644 index 0000000..e69de29 diff --git a/w3/Car.java b/w3/Car.java new file mode 100644 index 0000000..6a70709 --- /dev/null +++ b/w3/Car.java @@ -0,0 +1,113 @@ +public class Car { + // 1. 私有属性 + private final String licensePlate; // 车牌号(不可变) + private String brand; // 品牌 + private String model; // 型号 + private double dailyRent; // 日租金 + private boolean isRented; // 是否已出租 + + // 静态变量:统计车辆总数 + private static int totalCars = 0; + + // 2. 全参构造方法 + public Car(String licensePlate, String brand, String model, double dailyRent) { + this.licensePlate = licensePlate; + this.brand = brand; + this.model = model; + setDailyRent(dailyRent); // 通过setter校验租金 + this.isRented = false; // 初始状态:未出租 + totalCars++; // 每创建一个对象,总数+1 + } + + // 3. 三参构造方法(调用全参构造,日租金默认300元) + public Car(String licensePlate, String brand, String model) { + this(licensePlate, brand, model, 300.0); + } + + // 4. Getter/Setter 方法 + public String getLicensePlate() { + return licensePlate; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public double getDailyRent() { + return dailyRent; + } + + // 日租金 setter:必须校验是否大于0 + public void setDailyRent(double dailyRent) { + if (dailyRent > 0) { + this.dailyRent = dailyRent; + } else { + System.out.println("日租金必须大于0,设置失败!"); + } + } + + public boolean isRented() { + return isRented; + } + + // 注意:isRented 不提供 setter,只能通过业务方法修改状态 + + // 5. 业务方法 + // 租车:将状态置为已出租 + public void rentCar() { + if (isRented) { + System.out.println("车辆 " + licensePlate + " 已出租,无法再次租用!"); + } else { + isRented = true; + System.out.println("车辆 " + licensePlate + " 已成功出租!"); + } + } + + // 还车:将状态置为未出租 + public void returnCar() { + if (!isRented) { + System.out.println("车辆 " + licensePlate + " 未被租用,无需归还!"); + } else { + isRented = false; + System.out.println("车辆 " + licensePlate + " 已成功归还!"); + } + } + + // 计算总租金:日租金 × 天数(假设天数>0) + public double calculateRent(int days) { + if (days > 0) { + return dailyRent * days; + } else { + System.out.println("租赁天数必须大于0!"); + return 0.0; + } + } + + // 6. 静态方法:获取总车辆数 + public static int getTotalCars() { + return totalCars; + } + + // 辅助方法:打印车辆信息 + public void displayInfo() { + System.out.println("=== 车辆信息 ==="); + System.out.println("车牌号:" + licensePlate); + System.out.println("品牌:" + brand); + System.out.println("型号:" + model); + System.out.println("日租金:" + dailyRent + " 元/天"); + System.out.println("状态:" + (isRented ? "已出租" : "可租")); + System.out.println("================"); + } +} \ No newline at end of file diff --git a/w3/TestCar.java b/w3/TestCar.java new file mode 100644 index 0000000..6cb34e4 --- /dev/null +++ b/w3/TestCar.java @@ -0,0 +1,35 @@ +public class TestCar { + public static void main(String[] args) { + // 创建3个Car对象(使用不同构造方法) + Car car1 = new Car("京A12345", "丰田", "凯美瑞", 280.0); + Car car2 = new Car("沪B67890", "本田", "雅阁"); // 日租金默认300元 + Car car3 = new Car("粤C11111", "大众", "帕萨特", 320.0); + + // 打印所有车辆信息 + System.out.println("=== 初始车辆信息 ==="); + car1.displayInfo(); + car2.displayInfo(); + car3.displayInfo(); + + // 测试租车/还车业务 + System.out.println("\n=== 测试租车/还车 ==="); + car1.rentCar(); // 第一次租车 + car1.rentCar(); // 重复租车(提示已出租) + car1.returnCar(); // 第一次还车 + car1.returnCar(); // 重复还车(提示未出租) + + // 计算租金 + System.out.println("\n=== 计算租金 ==="); + double rent = car1.calculateRent(5); + System.out.println("车辆 " + car1.getLicensePlate() + " 租赁5天,总租金:" + rent + " 元"); + + // 测试setDailyRent非法值 + System.out.println("\n=== 测试日租金非法值 ==="); + car1.setDailyRent(-100); // 非法值,提示错误 + System.out.println("修改后日租金:" + car1.getDailyRent() + " 元/天"); + + // 测试静态变量 + System.out.println("\n=== 车辆总数 ==="); + System.out.println("当前系统中车辆总数:" + Car.getTotalCars() + " 辆"); + } +} \ No newline at end of file