python编写一个fan类
时间: 2024-03-25 18:40:24 浏览: 37
好的,下面是一个简单的Python代码实现Fan类:
``` python
class Fan:
# 构造函数
def __init__(self, speed=1, radius=5, color="blue", is_on=False):
self.__speed = speed # 风扇速度
self.__radius = radius # 风扇半径
self.__color = color # 风扇颜色
self.__is_on = is_on # 风扇状态
# 访问器方法
def get_speed(self):
return self.__speed
def get_radius(self):
return self.__radius
def get_color(self):
return self.__color
def is_on(self):
return self.__is_on
# 修改器方法
def set_speed(self, speed):
self.__speed = speed
def set_radius(self, radius):
self.__radius = radius
def set_color(self, color):
self.__color = color
def turn_on(self):
self.__is_on = True
def turn_off(self):
self.__is_on = False
# 输出风扇状态
def __str__(self):
if self.__is_on:
return "Speed: " + str(self.__speed) + ", Radius: " + str(self.__radius) + ", Color: " + self.__color + ", Fan is on"
else:
return "Radius: " + str(self.__radius) + ", Color: " + self.__color + ", Fan is off"
```
在上述代码中,我们定义了一个Fan类,其中包含了一些属性,如风扇的速度、半径、颜色和状态等。同时,我们也定义了一些方法,如获取和修改风扇的速度、半径、颜色和状态等。最后,我们还重载了类的`__str__`方法,以便输出风扇的状态信息。