用java写一个封装类车系 厂商 车类 品牌 车型 级别 价格 时间 销量 销售规模(亿) 省份 城市,并使用用注解,然后实现Comparabled接口
时间: 2024-03-01 07:53:52 浏览: 94
好的,我可以为您编写一个Java封装类来表示车系信息,并实现Comparable接口进行比较排序。下面是代码:
```java
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Column {
String value();
}
public class CarSeries implements Comparable<CarSeries> {
@Column("厂商")
private String manufacturer;
@Column("车类")
private String carType;
@Column("品牌")
private String brand;
@Column("车型")
private String model;
@Column("级别")
private String level;
@Column("价格")
private double price;
@Column("时间")
private int year;
@Column("销量")
private int salesVolume;
@Column("销售规模(亿)")
private double salesScale;
@Column("省份")
private String province;
@Column("城市")
private String city;
public CarSeries(String manufacturer, String carType, String brand, String model, String level, double price, int year, int salesVolume, double salesScale, String province, String city) {
this.manufacturer = manufacturer;
this.carType = carType;
this.brand = brand;
this.model = model;
this.level = level;
this.price = price;
this.year = year;
this.salesVolume = salesVolume;
this.salesScale = salesScale;
this.province = province;
this.city = city;
}
public String getManufacturer() {
return manufacturer;
}
public String getCarType() {
return carType;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public String getLevel() {
return level;
}
public double getPrice() {
return price;
}
public int getYear() {
return year;
}
public int getSalesVolume() {
return salesVolume;
}
public double getSalesScale() {
return salesScale;
}
public String getProvince() {
return province;
}
public String getCity() {
return city;
}
@Override
public int compareTo(CarSeries o) {
if (this.price > o.price) {
return 1;
} else if (this.price < o.price) {
return -1;
} else {
return 0;
}
}
}
```
这里我使用了注解@Column来表示每个字段的含义,这样可以方便地对车系信息进行管理。在compareTo方法中,我选择按照价格进行比较排序,您可以根据需要修改这个方法来实现其他的排序方式。
阅读全文