用java代码写出:有一个风扇类,有属性 形状和颜色,通过外部get/set方法 修改或获取 对应属性,并通过风扇的show方法,输出自己的形状和颜色
时间: 2023-04-03 08:02:55 浏览: 62
请问您需要我用 C 语言编写还是 Java 语言编写呢?如果是 Java 语言编写,可以使用以下代码实现:
public class Fan {
private String shape;
private String color;
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void show() {
System.out.println("风扇的形状是:" + shape + ",颜色是:" + color);
}
}
然后在主函数中可以这样使用:
Fan fan = new Fan();
fan.setShape("圆形");
fan.setColor("红色");
fan.show();
输出结果为:
风扇的形状是:圆形,颜色是:红色