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

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

问题描述

我收到一条错误消息,指出产品不包含采用 0 个参数的构造函数";来自以下代码:

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

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

推荐答案

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

Several rules about C# come into play here:

  1. 每个类都必须有一个构造函数(为了更好地构造)

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

如果您不提供构造函数,编译器会自动为您提供一个构造函数,无需更改.

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

这意味着类

class Demo{}

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

upon compilation is provided with an empty constructor, becoming

class Demo{
   public Demo(){}
}

我能做到

Demo instance = new Demo();

  • 如果您确实提供了构造函数(任何具有任何签名的构造函数),则不会生成空构造函数

  • 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
    

    这似乎有点违反直觉,因为添加代码似乎会破坏现有的不相关代码,但这是 C# 团队的设计决定,我们必须接受它.

    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.

    调用派生类的构造函数时,如果没有指定要调用的基类构造函数,编译器会调用空的基类构造函数,所以

    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(){}
    }
    

    变成

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

  • 所以,为了构造你的派生类,你必须在基类上有一个无参数的构造函数.看你怎么给Products加了一个构造函数,编译器没有生成默认的构造函数,你需要在你的代码中显式的添加,比如:

    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天全站免登陆