如何设置修饰符?我想让嵌套类中的“get”为所有人公开,只为外部类设置?错误:
索引器“Cart.CartItem.Quantity”的属性无法在此上下文中使用,因为设置访问器无法访问“Cart.CartItem.CartItem(Guid itemId, string name, decimal price, int quantity)”由于其保护级别而无法访问
代码:
public class Cart
{
public List<CartItem> CartItems { get; private set; }
public int TotalQuantity => CartItems.Sum(x => x.Quantity);
public decimal TotalPrice => CartItems.Sum(x => x.Price * x.Quantity);
public Cart()
{
CartItems = new List<CartItem>();
}
public void AddItem(Guid itemId, string name, decimal price)
{
CartItem cartItem = CartItems.Find(x => x.ItemId == itemId);
if (cartItem != null)
{
cartItem.Quantity += 1;
}
else
{
CartItems.Add(new CartItem(itemId, name, price, 1));
}
}
public class CartItem
{
public Guid ItemId { get; private set; }
public string Name { get; private set; }
public int Quantity { get; private set; }
public decimal Price { get; private set; }
private CartItem(Guid itemId, string name, decimal price, int quantity)
{
ItemId = itemId;
Name = name;
Price = price;
Quantity = quantity;
}
}
}
吃鸡游戏
相关分类