You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为售卖电脑配件与外设的商店创建产品类的技术需求

嘿,针对你说的这家售卖电脑配件和外设的商店,我给你整理一套清晰的面向对象类设计方案,用Python做示例(可读性强,也容易转换成其他OOP语言),后续扩展新产品或修改属性都很方便。

1. 先搭建通用基类:Product

所有产品不管是配件还是外设,肯定都有一些共同属性,比如名称、品牌、价格、库存这些,把这些抽成基类,避免重复写代码,符合DRY原则(Don't Repeat Yourself)。

class Product:
    def __init__(self, name: str, brand: str, price: float, stock: int):
        self.name = name
        self.brand = brand
        self.price = price
        self.stock = stock

    def display_info(self):
        """展示产品基本信息"""
        print(f"产品名称: {self.name}")
        print(f"品牌: {self.brand}")
        print(f"价格: {self.price}元")
        print(f"库存: {self.stock}件")
2. 电脑配件子类设计

每个配件都继承Product,再添加自己的特有属性,同时可以重写display_info方法来展示更多细节。

2.1 主板(Mobo)类

主板的核心属性是GPU类型、SATA接口数量,我们可以用枚举来限定可选值,避免输入无效内容:

from enum import Enum

class GPUType(Enum):
    AMD = "AMD"
    INTEL = "Intel"

class SATAInterfaceCount(Enum):
    FOUR = 4
    SIX = 6
    EIGHT = 8

class Motherboard(Product):
    def __init__(self, name: str, brand: str, price: float, stock: int, gpu_type: GPUType, sata_count: SATAInterfaceCount):
        super().__init__(name, brand, price, stock)
        self.gpu_type = gpu_type
        self.sata_count = sata_count

    def display_info(self):
        super().display_info()
        print(f"GPU类型: {self.gpu_type.value}")
        print(f"SATA接口数量: {self.sata_count.value}")

2.2 CPU类

CPU的特有属性是频率、核心数、是否集成显卡:

class CPU(Product):
    def __init__(self, name: str, brand: str, price: float, stock: int, frequency: float, core_count: int, has_integrated_gpu: bool):
        super().__init__(name, brand, price, stock)
        self.frequency = frequency  # 单位:GHz
        self.core_count = core_count
        self.has_integrated_gpu = has_integrated_gpu

    def display_info(self):
        super().display_info()
        print(f"频率: {self.frequency} GHz")
        print(f"核心数: {self.core_count}")
        print(f"集成显卡: {'是' if self.has_integrated_gpu else '否'}")

2.3 内存(RAM)类

内存的属性包括类型、容量、频率:

class RAMType(Enum):
    DDR2 = "DDR2"
    DDR3 = "DDR3"
    DDR4 = "DDR4"

class RAM(Product):
    def __init__(self, name: str, brand: str, price: float, stock: int, ram_type: RAMType, capacity: int, frequency: int):
        super().__init__(name, brand, price, stock)
        self.ram_type = ram_type
        self.capacity = capacity  # 单位:GB
        self.frequency = frequency  # 单位:MHz

    def display_info(self):
        super().display_info()
        print(f"内存类型: {self.ram_type.value}")
        print(f"容量: {self.capacity} GB")
        print(f"频率: {self.frequency} MHz")

2.4 显卡(GPU)类

显卡的属性是芯片组、显存:

class GPUChipset(Enum):
    AMD = "AMD"
    NVIDIA = "nVidia"

class GPU(Product):
    def __init__(self, name: str, brand: str, price: float, stock: int, chipset: GPUChipset, vram: int):
        super().__init__(name, brand, price, stock)
        self.chipset = chipset
        self.vram = vram  # 单位:GB

    def display_info(self):
        super().display_info()
        print(f"芯片组: {self.chipset.value}")
        print(f"显存: {self.vram} GB")

2.5 存储设备类

存储设备的属性是类型、尺寸、容量:

class StorageType(Enum):
    HDD = "HDD"
    SSD = "SSD"

class StorageSize(Enum):
    INCH_1_8 = 1.8
    INCH_2_5 = 2.5
    INCH_3_5 = 3.5

class StorageDevice(Product):
    def __init__(self, name: str, brand: str, price: float, stock: int, storage_type: StorageType, size: StorageSize, capacity: int):
        super().__init__(name, brand, price, stock)
        self.storage_type = storage_type
        self.size = size  # 单位:英寸
        self.capacity = capacity  # 单位:GB

    def display_info(self):
        super().display_info()
        print(f"存储类型: {self.storage_type.value}")
        print(f"尺寸: {self.size.value} 英寸")
        print(f"容量: {self.capacity} GB")
3. 外设类:显示器

显示器作为外设,同样继承Product,添加特有属性:

class DisplayType(Enum):
    LCD = "LCD"
    LED = "LED"

class Monitor(Product):
    def __init__(self, name: str, brand: str, price: float, stock: int, display_type: DisplayType):
        super().__init__(name, brand, price, stock)
        self.display_type = display_type

    def display_info(self):
        super().display_info()
        print(f"显示器类型: {self.display_type.value}")
设计小提示
  • 用枚举(Enum)来限定属性的可选值,能有效避免无效输入(比如把GPU类型写成小写的"amd",或者输入不存在的SATA接口数),让代码更健壮。
  • 如果后续要加新的产品类型(比如键盘、鼠标这类外设),只需要新建一个继承Product的子类,添加特有属性即可,扩展性很强。
  • 基类的display_info方法可以被所有子类重写,展示各自的细节,符合多态特性。

内容的提问来源于stack exchange,提问作者user8144114

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:07:29