不包含带有0个参数的构造函数 [英] Does not contain a constructor that takes 0 arguments

查看:589
本文介绍了不包含带有0个参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我得到一个错误,产品不包含一个参数为0的构造函数

  public class Products 
{
string id;
string name;
double price;
int soldCount;
int stockCount;

public Products id,string name,double price,
int soldCount,int stockCount,double tax)
{
this.id = id;
this.name = name;
this.price = price;
this.soldCount = soldCount;
this.stockCount = stockCount;
}
}

//我有一些获取和设置上面的代码
//的值,但是它已经太长了,不能放在这里

public class FoodProducts:产品
{
public FoodProduct )
{
Console.WriteLine(This is food product);
}

public void Limit()
{
Console .WriteLine(This is a Attribute of a Product);
}
}


解决方案

关于C#的规则在这里发挥作用:


  1. 每个类必须有一个构造函数p>


  2. 如果您不提供构造函数,那么将由编译器自动为您提供构造函数。



    这意味着类

      class Demo {} 



    编译时提供一个空构造函数,成为

      class Demo {
    public Demo(){}
    }

    我可以做

      Demo instance = new Demo(); 


  3. 如果你提供一个构造函数不产生

      class Demo {
    public Demo(int parameter){}
    }

    Demo instance = new Demo(); //此代码现在失败
    Demo instance = new Demo(3); //此代码现在成功

    这看起来有点反直觉,因为添加代码似乎


  4. 当你调用一个派生类的构造函数时,如果不指定要调用的基类构造函数,编译器将调用空基类构造函数,因此

      class Derived:基础{
    public Derived(){}
    }

    >

      class Derived:Base {
    public Derived():base(){}
    }


因此,为了构造你的派生类,无参数构造函数。看到你如何添加一个构造函数到产品,并且编译器没有生成默认构造函数,你需要显式地添加它在你的代码,如:

  public Products()
{
}

显式调用它从派生构造函数

  public FoodProduct()
:base(string.Empty,string.Empty, 0,0,0,0)
{
}


Seems that i get an error stating "Products does not contain a constructor that takes 0 arguments

public class Products
{
    string id;
    string name;
    double price;
    int soldCount;
    int stockCount;

    public Products(string id, string name, double price, 
                      int soldCount, int stockCount, double tax)
    {
        this.id = id;
        this.name = name;
        this.price = price;
        this.soldCount = soldCount;
        this.stockCount = stockCount;
    }
}

//I have got some get and set values for the code above 
//but it would have been too long to put in here

public class FoodProducts : Products
{
    public FoodProduct()
    {
        Console.WriteLine("This is food product");
    }

    public void Limit()
    {
        Console.WriteLine("This is an Attribute of a Product");
    }
}

解决方案

Several rules about C# come into play here:

  1. Each class must have a constructor (In order to be, well constructed)

  2. If you do not provide a constructor, a constructor will be provided for you, free of change, automatically by the compiler.

    This means that the class

    class Demo{}
    

    upon compilation is provided with an empty constructor, becoming

    class Demo{
       public Demo(){}
    }
    

    and I can do

    Demo instance = new Demo();
    

  3. If you do provide a constructor (any constructor with any signature), the empty constructor will not be generated

    class Demo{
       public Demo(int parameter){}
    }
    
    Demo instance = new Demo(); //this code now fails
    Demo instance = new Demo(3); //this code now succeeds
    

    This can seem a bit counter-intuitive, because adding code seems to break existing unrelated code, but it's a design decision of the C# team, and we have to live with it.

  4. When you call a constructor of a derived class, if you do not specify a base class constructor to be called, the compiler calls the empty base class constructor, so

    class Derived:Base {
       public Derived(){}
    }
    

    becomes

    class Derived:Base {
       public Derived() : base() {}
    }
    

So, in order to construct your derived class, you must have a parameterless constructor on the base class. Seeing how you added a constructor to the Products, and the compiler did not generate the default constructor, you need to explicitly add it in your code, like:

public Products()
{
}

or explicitly call it from the derived constructor

public FoodProduct()
       : base(string.Empty, string.Empty, 0, 0, 0, 0)
{
}

这篇关于不包含带有0个参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆