【问题标题】:nested class - get as public for all, set only for outer class嵌套类 - 对所有人公开,仅为外部类设置
【发布时间】:2019-08-04 01:23:09
【问题描述】:

如何设置修饰符?我想让嵌套类中的“get”为所有人公开,只为外部类设置? 错误:

索引器“Cart.CartItem.Quantity”的属性不能用于 这个上下文,因为 set 访问器是不可访问的 'Cart.CartItem.CartItem(Guid itemId, string name, decimal price, int 数量)'由于其保护级别而无法访问

代码:

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;            
        }
    }
}

【问题讨论】:

  • 产生这个错误的代码是什么?
  • @ZoharPeled,我认为这是一个错误 => cartItem.Quantity += 1; 因为setCartItem 类中对于Quantity 是私有的
  • @er-sho 是的,你可能是对的。

标签: c#


【解决方案1】:

您没有完全理解使用嵌套类型的原因。

嵌套类型可以访问封闭类型中定义的私有字段

查看关于Dos and Donts的链接

X 避免公开暴露的嵌套类型。唯一的例外是嵌套类型的变量仅在极少数情况下需要声明,例如子类化或其他高级自定义场景。

如果类型可能在包含类型之外被引用,则不要使用嵌套类型。

所以正确的做法是保持类私有而成员公开,因此嵌套类型的成员和字段只能由封闭类型访问

public class Cart {
    List<CartItem> CartItems { get; 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));
        }
    }

    class CartItem {
        public Guid ItemId { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }

        public CartItem(Guid itemId, string name, decimal price, int quantity) {
            ItemId = itemId;
            Name = name;
            Price = price;
            Quantity = quantity;
        }
    }
}

class Program {
    static void Main(string[] args) {
        var test = new Cart.CartItem(Guid.Empty, "", 0.0m, 10); // Error CS0122  'Cart.CartItem' is inaccessible due to its protection level 


    }
}

【讨论】:

  • 它应该编译除了Main中的代码,它将显示注释错误
猜你喜欢
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 2018-10-30
  • 2013-09-05
  • 2011-01-10
  • 1970-01-01
  • 2014-10-30
  • 2017-01-25
相关资源
最近更新 更多