定义一个计算机类mycomputer
时间: 2023-08-10 09:07:24 浏览: 146
以下是一个示例的计算机类 MyComputer 的定义:
```python
class MyComputer:
def __init__(self, brand, model, cpu, ram, storage):
self.brand = brand
self.model = model
self.cpu = cpu
self.ram = ram
self.storage = storage
def get_specs(self):
print(f"Brand: {self.brand}")
print(f"Model: {self.model}")
print(f"CPU: {self.cpu}")
print(f"RAM: {self.ram}")
print(f"Storage: {self.storage}")
```
这个类有五个属性:品牌(brand)、型号(model)、CPU、内存(RAM)和存储(storage)。它还有一个名为 get_specs 的方法,用于打印计算机的规格信息。
在实例化 MyComputer 类时,需要提供相应的属性值。例如:
```python
my_computer = MyComputer("Dell", "XPS 13", "Intel Core i7", "16GB", "512GB SSD")
```
这将创建一个 MyComputer 类的实例,其属性值为:
- 品牌:Dell
- 型号:XPS 13
- CPU:Intel Core i7
- 内存:16GB
- 存储:512GB SSD
可以通过调用 get_specs 方法来打印这些规格信息:
```python
my_computer.get_specs()
```
这将输出:
```
Brand: Dell
Model: XPS 13
CPU: Intel Core i7
RAM: 16GB
Storage: 512GB SSD
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)