温馨提示×

Java SpareTire类使用实例

小云
84
2023-09-28 10:20:39
栏目: 编程语言

下面是一个Java SpareTire类的实例:

public class SpareTire {
private int diameter;
private String brand;
public SpareTire(int diameter, String brand) {
this.diameter = diameter;
this.brand = brand;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void inflate() {
System.out.println("Inflating spare tire");
}
public void changeBrand(String newBrand) {
System.out.println("Changing spare tire brand from " + brand + " to " + newBrand);
this.brand = newBrand;
}
public static void main(String[] args) {
// 创建一个SpareTire对象
SpareTire spareTire = new SpareTire(16, "Goodyear");
// 调用对象的方法
System.out.println("Spare tire diameter: " + spareTire.getDiameter());
System.out.println("Spare tire brand: " + spareTire.getBrand());
spareTire.inflate();
spareTire.changeBrand("Michelin");
System.out.println("New spare tire brand: " + spareTire.getBrand());
}
}

这个SpareTire类表示一个备用轮胎,它有一个直径和品牌属性,以及一些操作这些属性的方法。在main方法中,我们创建一个SpareTire对象,并调用它的方法来获取和修改属性,并执行一些其他操作。最后,我们打印出修改后的属性值。

0