【问题标题】:CS7036:There is no argument given that corresponds to the required formal parameter 'i' [duplicate]CS7036:没有给出与所需形式参数“i”相对应的参数 [重复]
【发布时间】:2021-07-18 09:27:09
【问题描述】:

我刚学C#,用构造函数做了两个外部类,一个继承自另一个。但它给出了错误: 严重性代码 描述 项目文件行抑制状态 错误 CS7036 没有给出与 'Engineer.Engineer(string)' program.cs C:\Users\win 10\Desktop\C#\program.cs\program.cs\Car 所需的形参 'i' 对应的参数.cs 41 活动 三个代码文件是: 1/ main.cs:

using System;

namespace program
{
    class Core
    {
        static void Main(string[] args)
        {
            Car BMW = new Car("X-A-21-A-X", 3200000, "Reddish-brown", false);
            string currentPrice = BMW.CheckPrice("us", BMW.price);
            if(!double.TryParse(currentPrice, out var q))
            {
                Console.WriteLine(currentPrice);
            }else if(double.TryParse(currentPrice, out var z))
            {
                double converted_Price = Convert.ToDouble(currentPrice);
                Console.WriteLine(converted_Price);
            }
            Console.WriteLine(BMW.model);
        }
    }
}

2/ Car.cs:

using System;
namespace program
{
    class Car : Engineer
    {
        private string _model;
        public string model
        {
            get { return _model; }
            set { _model = value; }
        }
        public double price;
        public string color;
        public bool available;
        public string CheckPrice(string locale, double price)
        {
            string ret = default(string);
            if(locale == "in")// India
            {
                ret = Convert.ToString(2.14 * price);
            }else if(locale == "us")// USA
            {
                ret = Convert.ToString(3.98 * price);
            }else if(locale == "jp")// Japan
            {
                ret = Convert.ToString(1.3 * price);
            }else if(locale == "vn")//Vietnam
            {
                ret = Convert.ToString(0.78645 * price);
            }else if(locale == "ch")//China
            {
                ret = Convert.ToString(2.56 * price);
            }
            else
            {
                ret = "Invalid Locale, Your Country does not ship the car.";
            }
            Console.WriteLine(_model);
            return ret;
        }
        public Car(string modelName, double priceVal, string ColorName, bool avail) /* 'Car' in this line is causing problems*/
        {
            model = modelName;
            price = priceVal;
            color = ColorName;
            available = avail;
        }
    }
}

3/ Engineer.cs:

using System;

namespace program
{
    class Engineer
    {
        private string creatorCompany;
        public string creator_Company
        {
            get { return creatorCompany; }
            set { creatorCompany = value; }
        }
        public Engineer(string i)
        {
            creator_Company = i;
        }
    }
}

那里有答案,但我无法理解。请给我解释一下,就像我是一个不知道sh*t的和尚一样

【问题讨论】:

  • 尝试在工程师类public Engineer() { }添加默认构造函数@
  • @viveknuna 我已经添加了

标签: c# inheritance parameters constructor public


【解决方案1】:

您需要将默认构造函数添加到Engineer 类。因为当你创建一个派生实例时,它会在派生类构造函数之前调用基类构造函数。

public Engineer()
{
}

【讨论】:

    【解决方案2】:

    如果CarEngineer

    CarEngineer 的不太可能的情况下,Car 需要提供creatorCompany

    1. Engineer 定义声明必须提供 creatorCompany
    2. Car Engineer
    3. Car 必须提供creatorCompany

    它可能看起来像这样:

    public Car(
      string creatorCompany, // Added
      string modelName, 
      double priceVal, 
      string ColorName, 
      bool avail) 
    : base(i: creatorCompany) // Added
    {
       model = modelName;
       price = priceVal;
       color = ColorName;
       available = avail;
    }
    

    如果Car 不是Engineer

    在这种情况下,解决方法是删除: Engineer

    class Car : Engineer
    

    变成:

    class Car
    

    【讨论】:

      【解决方案3】:

      我找到了答案,我只需要将engineer的构造函数类添加到car。我从 Engineer 文件中删除了构造函数,并在“Car”的构造函数中添加了另一个参数并将其设置在那里。

      【讨论】:

      • 汽车成为工程师很奇怪。
      • 我只是在学习 C#,只是在做实验
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 2016-05-13
      • 1970-01-01
      相关资源
      最近更新 更多