OOPAndPOP/ShoppingCart.cs

(完整代码) 1KB

using System.Collections.Generic;

namespace DesignModeConsole.OOPAndPOP;

/*
 本文讲述那些因为滥用get、set所导致的像面向对象,但是实际上是面向过程的行为
 */

/// <summary>
/// 面向过程
/// </summary>
public class ShoppingCart1
{
    public int ItemCount { get; set; }

    public double ToTalPrice { get; set; }

    public List<ShoppingCartItem> Items { get; set; }
}

/// <summary>
/// 面向对象
/// </summary>
public class ShoppingCart2
{
    public int ItemCount { get; private set; }

    public double ToTalPrice { get; private set; }

    public List<ShoppingCartItem> Items { get; }

    public IReadOnlyCollection<ShoppingCartItem> GetItems()
    {
        return Items;
    }

    public void AddItem(ShoppingCartItem item)
    {
        Items.Add(item);
        ItemCount++;
        ToTalPrice += item.Price;
    }

    public void Clear()
    {
        Items.Clear();
        ToTalPrice = 0;
        ItemCount = 0;
    }
}

public class ShoppingCartItem
{
    public string ShopId { get; set; }

    public double Price { get; set; }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。