C# CS7036:未给出与所需形式参数';i';
我只是在学习C#,我用构造函数创建了两个外部类,一个是从另一个继承的。但它给出了一个错误: 严重性代码说明项目文件行抑制状态 错误CS7036未给出与“Engineer.Engineer(string)”program.cs C:\Users\win 10\Desktop\C#\program.cs\program.cs\Car.cs 41 Active的必需形式参数“i”对应的参数 这三个代码文件是: 1/main.cs:C# CS7036:未给出与所需形式参数';i';,c#,inheritance,parameters,constructor,public,C#,Inheritance,Parameters,Constructor,Public,我只是在学习C#,我用构造函数创建了两个外部类,一个是从另一个继承的。但它给出了一个错误: 严重性代码说明项目文件行抑制状态 错误CS7036未给出与“Engineer.Engineer(string)”program.cs C:\Users\win 10\Desktop\C#\program.cs\program.cs\Car.cs 41 Active的必需形式参数“i”对应的参数 这三个代码文件是: 1/main.cs: using System; namespace program {
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的蒙克人一样。您需要将默认构造函数添加到
Engineer
类中。因为当您创建派生类的实例时,它会在派生类构造函数之前调用基类构造函数
public Engineer()
{
}
如果汽车
是工程师
在Car
不太可能是Engineer
的情况下,Car
需要提供creatorCompany
:
Engineer
定义规定必须提供creatorCompany
汽车
是工程师
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
我找到了答案,我只需要将
engineer
的构造函数类添加到car
。我从工程师文件中删除了构造函数,并将另一个参数添加到“Car”的构造函数中,并将其设置在那里。尝试在工程师类中添加默认构造函数public Engineer(){}
@viveknuna我知道adddcar作为工程师很奇怪。我只是在学习C#,只是在试验