编写java程序,该程序中有两个类:Vehicle(用于刻画机动车),和User类
时间: 2023-12-24 08:05:04 浏览: 159
以下是可能的Java程序示例:
```java
public class Vehicle {
private String make;
private String model;
private int year;
private int mileage;
public Vehicle(String make, String model, int year, int mileage) {
this.make = make;
this.model = model;
this.year = year;
this.mileage = mileage;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
}
public class User {
private String name;
private int age;
private Vehicle vehicle;
public User(String name, int age, Vehicle vehicle) {
this.name = name;
this.age = age;
this.vehicle = vehicle;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Vehicle getVehicle() {
return vehicle;
}
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
}
```
这个程序定义了两个类:Vehicle 和 User。Vehicle用于表示机动车,包括车辆品牌、型号、年份和里程数等信息。User类则表示用户,包括用户姓名、年龄和所拥有的机动车等信息。
在这个程序中,User类中包含一个Vehicle类型的属性,以表示用户所拥有的机动车。可以通过调用User和Vehicle类中的方法来访问和修改它们的属性。
阅读全文