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

如何基于已有类定义类?电商网站类设计及搜索实现咨询

嘿,针对你搭建在线购物网站遇到的类设计和搜索功能问题,我来给你梳理下可行的方案~

一、优化类的声明方式(面向对象重构)

首先,你当前的Shirt和Book类采用了组合(包含Item对象)的方式,但其实更合理的是用继承——因为衬衫、书籍本质上都是商品,天然具备Item的所有属性,同时拥有自己的特有属性。这样做的好处是:

  • 代码复用性更强,不用重复写Item的属性和方法
  • 所有商品都可以统一归为Item类型,方便后续统一管理(比如用一个集合存储所有商品)
  • 支持多态,为后续扩展其他商品类型(比如Electronics)提供便利

下面是重构后的代码示例(以Java为例,你可以根据自己用的语言调整):

1. 父类Item(通用商品属性)

把通用属性设为protected(或者提供公共的getter方法),方便子类访问:

public class Item {
    protected int price;
    protected String title;
    protected String description;
    protected String category;

    // 建议添加构造方法,初始化通用属性
    public Item(int price, String title, String description, String category) {
        this.price = price;
        this.title = title;
        this.description = description;
        this.category = category;
    }

    // 为通用属性添加getter方法,方便后续搜索等操作
    public int getPrice() { return price; }
    public String getTitle() { return title; }
    public String getDescription() { return description; }
    public String getCategory() { return category; }
}

2. 子类Shirt(衬衫商品)

通过extends继承Item,只需要定义自己的特有属性:

public class Shirt extends Item {
    private String size;
    private String color;

    // 构造方法:先调用父类构造初始化通用属性,再初始化特有属性
    public Shirt(int price, String title, String description, String category, String size, String color) {
        super(price, title, description, category);
        this.size = size;
        this.color = color;
    }

    // 特有属性的getter方法
    public String getSize() { return size; }
    public String getColor() { return color; }
}

3. 子类Book(书籍商品)

同理,继承Item并添加特有属性:

public class Book extends Item {
    private String author;
    private int noOfPages;

    public Book(int price, String title, String description, String category, String author, int noOfPages) {
        super(price, title, description, category);
        this.author = author;
        this.noOfPages = noOfPages;
    }

    public String getAuthor() { return author; }
    public int getNoOfPages() { return noOfPages; }
}
二、实现搜索功能

基于上面的类结构,我们可以实现两种类型的搜索:全局通用搜索(基于所有商品的通用属性)和特定品类精准搜索(基于子类特有属性)。

首先,先准备一个存储所有商品的集合:

import java.util.ArrayList;
import java.util.List;

public class ShoppingSite {
    public static void main(String[] args) {
        // 存储所有在售商品
        List<Item> allItems = new ArrayList<>();
        // 添加示例商品
        allItems.add(new Shirt(59, "纯棉圆领T恤", "舒适透气亲肤", "服装", "M", "白色"));
        allItems.add(new Shirt(79, "牛仔衬衫", "复古水洗工艺", "服装", "L", "蓝色"));
        allItems.add(new Book(39, "Java入门指南", "零基础快速上手", "图书", "李小明", 256));
        allItems.add(new Book(59, "算法图解", "可视化讲解算法", "图书", "王大锤", 180));
    }
}

1. 通用搜索示例

按标题关键词搜索

// 搜索标题包含指定关键词的所有商品(不区分大小写)
public List<Item> searchByTitleKeyword(List<Item> items, String keyword) {
    List<Item> result = new ArrayList<>();
    for (Item item : items) {
        if (item.getTitle().toLowerCase().contains(keyword.toLowerCase())) {
            result.add(item);
        }
    }
    return result;
}

按价格区间搜索

// 搜索价格在[minPrice, maxPrice]范围内的所有商品
public List<Item> searchByPriceRange(List<Item> items, int minPrice, int maxPrice) {
    List<Item> result = new ArrayList<>();
    for (Item item : items) {
        if (item.getPrice() >= minPrice && item.getPrice() <= maxPrice) {
            result.add(item);
        }
    }
    return result;
}

2. 特定品类搜索示例

搜索指定颜色的衬衫

需要先判断商品类型是否为Shirt,再强转后过滤:

public List<Shirt> searchShirtsByColor(List<Item> items, String color) {
    List<Shirt> result = new ArrayList<>();
    for (Item item : items) {
        if (item instanceof Shirt) {
            Shirt shirt = (Shirt) item;
            if (shirt.getColor().equalsIgnoreCase(color)) {
                result.add(shirt);
            }
        }
    }
    return result;
}

搜索指定作者的书籍

public List<Book> searchBooksByAuthor(List<Item> items, String author) {
    List<Book> result = new ArrayList<>();
    for (Item item : items) {
        if (item instanceof Book) {
            Book book = (Book) item;
            if (book.getAuthor().equalsIgnoreCase(author)) {
                result.add(book);
            }
        }
    }
    return result;
}

3. 组合条件搜索

你还可以结合多个条件,比如搜索价格在40-80之间的服装类商品:

public List<Item> searchByCategoryAndPriceRange(List<Item> items, String category, int minPrice, int maxPrice) {
    List<Item> result = new ArrayList<>();
    for (Item item : items) {
        if (item.getCategory().equalsIgnoreCase(category) 
            && item.getPrice() >= minPrice 
            && item.getPrice() <= maxPrice) {
            result.add(item);
        }
    }
    return result;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:38:41