Does not contain a constructor that takes 0 arguments

后端 未结 5 1543
悲哀的现实
悲哀的现实 2020-11-30 03:33

I get an error stating "Products does not contain a constructor that takes 0 arguments" from the following code:

public class Products
{
    string          


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 04:21

    Since Products has no constructor that takes 0 arguments, you must create a constructor for FoodProducts that calls the constructor of Products will all the required arguments.

    In C#, this is done like the following:

    public class FoodProducts : Products
    {
    
        public FoodProducts(string id, string name, double price, int soldCount, int stockCount, double tax)   
        : base(id, name, price, soldCount, stockCount, tax)
        {
        }
    
        public void Limit()
        {
            Console.WriteLine("This is an Attribute of a Product");
        }
    }
    

    If you don't want to add this constructor to FoodProducts, you can also create a constructor with no parameter to Products.

提交回复
热议问题