From 74e15a5559873f3413469328d9a8be99ea5148dd Mon Sep 17 00:00:00 2001 From: JianXinyi <1259606552@qq.com> Date: Mon, 30 Mar 2026 21:59:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'experiment1'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- experiment1/Car.java | 142 +++++++++++++++++++++++++++++++ experiment1/TestCar.java | 49 +++++++++++ experiment1/实验一报告.docx | Bin 0 -> 304458 bytes experiment1/实验一类图.png | Bin 0 -> 285092 bytes 4 files changed, 191 insertions(+) create mode 100644 experiment1/Car.java create mode 100644 experiment1/TestCar.java create mode 100644 experiment1/实验一报告.docx create mode 100644 experiment1/实验一类图.png diff --git a/experiment1/Car.java b/experiment1/Car.java new file mode 100644 index 0000000..cb21ec1 --- /dev/null +++ b/experiment1/Car.java @@ -0,0 +1,142 @@ +package com.rental; + +public class Car { + // 1. 私有属性 + private final String licensePlate; // 车牌号,使用 final 确保不可变 + private String brand; + private String model; + private double dailyRent; + private boolean isRented; + + // 5. 静态成员(加分项) + private static int totalCars = 0; // 用于统计创建的车辆总数 + + // 2. 构造方法 + /** + * 全参构造方法 + * @param licensePlate 车牌号 + * @param brand 品牌 + * @param model 型号 + * @param dailyRent 日租金 + */ + public Car(String licensePlate, String brand, String model, double dailyRent) { + this.licensePlate = licensePlate; + this.brand = brand; + this.model = model; + // 调用 setter 以进行租金校验 + this.setDailyRent(dailyRent); + this.isRented = false; // 初始化为未租出 + totalCars++; // 创建对象时,总数加1 + } + + /** + * 部分参数构造方法(车牌、品牌、型号) + * 日租金默认为 300.0 + * @param licensePlate 车牌号 + * @param brand 品牌 + * @param model 型号 + */ + public Car(String licensePlate, String brand, String model) { + // 使用 this() 调用全参构造方法 + this(licensePlate, brand, model, 300.0); + } + + // 3. Getter 和 Setter 方法 + public String getLicensePlate() { + return licensePlate; + } + // 车牌号没有 setter + + 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; + } + + public void setDailyRent(double dailyRent) { + // 数据校验:日租金必须大于0 + if (dailyRent > 0) { + this.dailyRent = dailyRent; + } else { + System.out.println("[警告] 日租金必须大于0,修改失败。当前值保持为:" + this.dailyRent); + } + } + + public boolean isRented() { + return isRented; + } + // isRented 没有 setter,状态必须通过业务方法改变 + + // 4. 业务方法 + /** + * 租车方法 + */ + public void rentCar() { + if (!this.isRented) { + this.isRented = true; + System.out.println("车牌号 " + this.licensePlate + " 的车辆已成功租出。"); + } else { + System.out.println("车辆已租出,无法再次租用。"); + } + } + + /** + * 还车方法 + */ + public void returnCar() { + if (this.isRented) { + this.isRented = false; + System.out.println("车牌号 " + this.licensePlate + " 的车辆已成功归还。"); + } else { + System.out.println("车辆未被租用,无需归还。"); + } + } + + /** + * 计算租金 + * @param days 租用天数 + * @return 总租金 + */ + public double calculateRent(int days) { + // 假设 days 参数合法(>0),按作业要求暂不校验 + return this.dailyRent * days; + } + + // 5. 静态方法 + /** + * 获取创建的车辆总数 + * @return 车辆总数 + */ + public static int getTotalCars() { + return totalCars; + } + + // 可选:一个用于打印车辆信息的方法,便于测试 + /** + * 显示车辆信息 + */ + public void displayInfo() { + String status = this.isRented ? "已租出" : "可租"; + System.out.println("=== 车辆信息 ==="); + System.out.println("车牌号: " + this.licensePlate); + System.out.println("品牌型号: " + this.brand + " - " + this.model); + System.out.printf("日租金: %.2f 元/天\n", this.dailyRent); + System.out.println("状态: " + status); + System.out.println("================="); + } + } + diff --git a/experiment1/TestCar.java b/experiment1/TestCar.java new file mode 100644 index 0000000..513cca8 --- /dev/null +++ b/experiment1/TestCar.java @@ -0,0 +1,49 @@ +package com.rental; + +public class TestCar { + public static void main(String[] args) { + System.out.println("===== 开始测试 Car 类 ====="); + + // 1. 创建至少3个Car对象(使用不同构造方法) + System.out.println("\n1. 创建车辆对象:"); + Car car1 = new Car("京A12345", "丰田", "凯美瑞", 450.0); + Car car2 = new Car("沪B56789", "宝马", "X5"); // 使用默认租金300 + Car car3 = new Car("粤C99999", "特斯拉", "Model 3", 600.0); + + // 显示初始信息 + car1.displayInfo(); + car2.displayInfo(); + car3.displayInfo(); + + // 2. 测试租车、还车业务逻辑 + System.out.println("\n2. 测试租车与还车业务:"); + System.out.println("--- 测试 car1 (正常流程) ---"); + car1.rentCar(); // 第一次租,应成功 + car1.rentCar(); // 第二次租,应提示“已租出” + car1.returnCar(); // 第一次还,应成功 + car1.returnCar(); // 第二次还,应提示“无需归还” + + System.out.println("\n--- 测试 car2 (直接尝试归还) ---"); + car2.returnCar(); // 未租就还,应提示“无需归还” + + // 3. 测试 calculateRent 方法 + System.out.println("\n3. 测试租金计算:"); + double rent = car3.calculateRent(5); + System.out.printf("车辆 %s 租用5天的费用为:%.2f 元\n", car3.getLicensePlate(), rent); + + // 4. 测试 setter 数据校验 (日租金) + System.out.println("\n4. 测试日租金Setter的数据校验:"); + System.out.println("car2当前日租金:" + car2.getDailyRent()); + car2.setDailyRent(350.0); // 合法修改 + System.out.println("修改为350.0后,car2日租金:" + car2.getDailyRent()); + car2.setDailyRent(-100.0); // 非法修改,应提示警告并保持原值 + System.out.println("尝试修改为-100.0后,car2日租金:" + car2.getDailyRent()); + + // 5. 测试静态变量(车辆总数) + System.out.println("\n5. 测试静态成员(车辆总数统计):"); + System.out.println("当前已创建的车辆总数为:" + Car.getTotalCars()); + + System.out.println("\n===== 测试结束 ====="); + } + } + diff --git a/experiment1/实验一报告.docx b/experiment1/实验一报告.docx new file mode 100644 index 0000000000000000000000000000000000000000..f389dce63133f6bf2eb45ff130f7ec13ebf2ae7c GIT binary patch literal 304458 zcmeGDbx>r%v+s-I?(Xi+0E4@`yAKRDxVyW%yTjn_OyfSdyEEwE&TZzmciesA-G5%h zi?}c1oQ~+GyLzqFwX!lRzxk;`NfsOe0|W{L1_T6z6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