用JAVA定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值
时间: 2024-05-05 14:15:06 浏览: 166
以下为JAVA代码:
```
public class Car {
private String color;
private String brand;
private String licensePlate;
private double price;
public Car(String color, String brand, String licensePlate, double price) {
this.color = color;
this.brand = brand;
this.licensePlate = licensePlate;
this.price = price;
}
public String getColor() {
return color;
}
public String getBrand() {
return brand;
}
public String getLicensePlate() {
return licensePlate;
}
public double getPrice() {
return price;
}
public static void main(String[] args) {
Car car1 = new Car("red", "Toyota", "ABC123", 10000.50);
Car car2 = new Car("blue", "Honda", "XYZ789", 20000.75);
System.out.println("Car 1:");
System.out.println("Color: " + car1.getColor());
System.out.println("Brand: " + car1.getBrand());
System.out.println("License Plate: " + car1.getLicensePlate());
System.out.println("Price: $" + car1.getPrice());
System.out.println();
System.out.println("Car 2:");
System.out.println("Color: " + car2.getColor());
System.out.println("Brand: " + car2.getBrand());
System.out.println("License Plate: " + car2.getLicensePlate());
System.out.println("Price: $" + car2.getPrice());
}
}
```
输出结果为:
```
Car 1:
Color: red
Brand: Toyota
License Plate: ABC123
Price: $10000.5
Car 2:
Color: blue
Brand: Honda
License Plate: XYZ789
Price: $20000.75
```
阅读全文