我有以下代码:
class Vehicle {
String make;
String model;
int manufactureYear;
int vehicleAge;
String color;
Vehicle({
this.make,
this.model,
this.manufactureYear,
this.color,
});
int get age {
return vehicleAge;
}
void set age(int currentYear) {
vehicleAge = currentYear - manufactureYear;
}
}
void main() {
Vehicle car = Vehicle(
make: "Honda", model: "Civic", manufactureYear: 2005, color: "red");
print("Car make: " + car.make); // output: Honda
print("Car model: " + car.model); // output: Civic
car.age = 2012; // set age
print(car.age); // output: calculated based on car.age value (7 in this case)
print("Car color: " + car.color);
}
输出如下:
Car make: Honda
Car model: Civic
7
Car color: red
问题:我想打印“汽车年龄:7”而不是“7”,实现这一目标的最佳方法是什么?