java创建一个租车公司类,其中包含私有属性车辆编号、车型和 价格,然后创建方法去访问和修改这些属性的测试类
时间: 2024-05-14 22:16:00 浏览: 81
下面是一个基本的租车公司类的实现:
```java
public class CarRentalCompany {
private int carId;
private String carModel;
private double price;
public CarRentalCompany(int carId, String carModel, double price) {
this.carId = carId;
this.carModel = carModel;
this.price = price;
}
public int getCarId() {
return carId;
}
public void setCarId(int carId) {
this.carId = carId;
}
public String getCarModel() {
return carModel;
}
public void setCarModel(String carModel) {
this.carModel = carModel;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
然后我们可以创建一个测试类来测试这个类的各个方法:
```java
public class CarRentalCompanyTest {
public static void main(String[] args) {
CarRentalCompany car1 = new CarRentalCompany(1, "Toyota Camry", 150.0);
System.out.println("Car " + car1.getCarId() + ": " + car1.getCarModel() + ", $" + car1.getPrice() + " per day");
// 修改车辆信息
car1.setPrice(200.0);
System.out.println("New price for Car " + car1.getCarId() + ": $" + car1.getPrice() + " per day");
}
}
```
输出结果:
```
Car 1: Toyota Camry, $150.0 per day
New price for Car 1: $200.0 per day
```
这样我们就可以创建一个租车公司类,并且使用测试类对其进行测试了。
阅读全文