猿问

嵌套类 - 对所有人公开,仅为外部类设置

如何设置修饰符?我想让嵌套类中的“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;            

        }

    }

}


MYYA
浏览 83回答 1
1回答

吃鸡游戏

您没有完全遵循使用嵌套类型的原因。嵌套类型可以访问封闭类型中定义的私有字段查看有关Dos and Donts的链接X 避免公开暴露的嵌套类型。唯一的例外是嵌套类型的变量只需要在极少数情况下声明,例如子类化或其他高级定制场景。X 如果类型可能在包含类型之外被引用,则不要使用嵌套类型。因此,正确的方法是保持类私有和成员公开,因此嵌套类型成员和字段只能由封闭类型访问public class Cart {&nbsp; &nbsp; List<CartItem> CartItems { get; set; }&nbsp; &nbsp; public int TotalQuantity => CartItems.Sum(x => x.Quantity);&nbsp; &nbsp; public decimal TotalPrice => CartItems.Sum(x => x.Price * x.Quantity);&nbsp; &nbsp; public Cart() {&nbsp; &nbsp; &nbsp; &nbsp; CartItems = new List<CartItem>();&nbsp; &nbsp; }&nbsp; &nbsp; public void AddItem(Guid itemId, string name, decimal price) {&nbsp; &nbsp; &nbsp; &nbsp; CartItem cartItem = CartItems.Find(x => x.ItemId == itemId);&nbsp; &nbsp; &nbsp; &nbsp; if (cartItem != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cartItem.Quantity += 1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CartItems.Add(new CartItem(itemId, name, price, 1));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class CartItem {&nbsp; &nbsp; &nbsp; &nbsp; public Guid ItemId { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int Quantity { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public decimal Price { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public CartItem(Guid itemId, string name, decimal price, int quantity) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemId = itemId;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Price = price;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Quantity = quantity;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}class Program {&nbsp; &nbsp; static void Main(string[] args) {&nbsp; &nbsp; &nbsp; &nbsp; var test = new Cart.CartItem(Guid.Empty, "", 0.0m, 10); // Error CS0122&nbsp; 'Cart.CartItem' is inaccessible due to its protection level&nbsp;&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答