java定义一个汽车类,属性包括车牌号、车主、核载人数、自重、车速
时间: 2023-10-26 17:05:32 浏览: 88
以下是一个简单的Java汽车类的实现:
```java
public class Car {
private String licensePlateNumber; // 车牌号
private String owner; // 车主
private int capacity; // 核载人数
private double weight; // 自重
private double speed; // 车速
// 构造方法
public Car(String licensePlateNumber, String owner, int capacity, double weight, double speed) {
this.licensePlateNumber = licensePlateNumber;
this.owner = owner;
this.capacity = capacity;
this.weight = weight;
this.speed = speed;
}
// getter和setter方法
public String getLicensePlateNumber() {
return licensePlateNumber;
}
public void setLicensePlateNumber(String licensePlateNumber) {
this.licensePlateNumber = licensePlateNumber;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
}
```
在上面的代码中,我们定义了一个名为`Car`的类,并声明了该类的五个属性:`licensePlateNumber`(车牌号)、`owner`(车主)、`capacity`(核载人数)、`weight`(自重)和`speed`(车速)。我们还提供了一个构造方法和getter和setter方法来访问这些属性。
阅读全文