设计一组类,分别表示计算机,台式机,笔记本,服务器
时间: 2023-11-16 10:06:50 浏览: 77
计算机类.
好的,为您设计一组类:
```
class Computer:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def turn_on(self):
print("Computer is turning on...")
def turn_off(self):
print("Computer is turning off...")
class Desktop(Computer):
def __init__(self, brand, model, monitor):
super().__init__(brand, model)
self.monitor = monitor
def play_game(self):
print("Playing game on desktop...")
class Laptop(Computer):
def __init__(self, brand, model, weight):
super().__init__(brand, model)
self.weight = weight
def carry_around(self):
print("Carrying laptop around...")
class Server(Computer):
def __init__(self, brand, model, rack_mounted):
super().__init__(brand, model)
self.rack_mounted = rack_mounted
def process_requests(self):
print("Processing requests on server...")
```
以上是一个简单的类设计,其中 `Computer` 是基类,其他三个类都继承自它。`Desktop`、`Laptop`、`Server` 分别表示台式机、笔记本和服务器,它们都有一些特殊的属性和方法。您可以在此基础上进一步扩展和优化。
阅读全文