解决错误“没有给出对应于所需形式参数的参数"? [英] Resolve error 'there is no argument given that corresponds to required formal parameter'?
问题描述
我有以下代码,在 C# Visual Studio 2015 中编译时出现错误.
I have following code where I'm getting error while compiling in C# visual Studio 2015.
class Oval:Shape
{
private double major_axis, minor_axis;
public Oval(double Major_Axis, double Minor_Axis)
{
major_axis = Major_Axis;
minor_axis = Minor_Axis;
} //Constructor
}
class Circle:Oval
{
private double radius;
public Circle(double Circle_Radius) // Getting Error on this line
{
radius = Circle_Radius;
} //constructor
}
推荐答案
修复您的错误:
错误是由于缺少无参数构造函数(或者您在构造函数中没有使用 base()
方法(就像 user3185569
所说的那样)
The error occurs due to the lack of a parameterless constructor (or your lack of using the base()
method in your constructor (just like user3185569
had said)
修复您的代码:
很明显,您似乎缺乏 .NET 的一些基础知识,因此我决定重新编写您的代码,并牢记以下几点:
It clearly seems you are lacking some basics in .NET so I've decided to give a re-writing to your code with the following things in mind:
一个.约定
有一些关于适用于您的代码的通用约定的规则.
There are some rules about common conventions that should apply to your code.
成员通常以 m
或 _
开头,然后是 memberName
(驼峰式大小写).
Members usually begin with either m
or _
and then the memberName
(camel casing).
属性通常被写成PropertyName
,同样适用于方法.
Properties are usually written regularly as PropertyName
and same applies to methods.
参数和变量只是像parameterName
B.访问修饰符
我不知道您的 Oval 和 circle 的用途,但我假设您想在 Oval
和 Circle
之外访问它们.
I don't know the use of your Oval and circle but I assume you'd want to access them outside of Oval
and Circle
.
我认为最好在此处参考您以阅读有关该主题的更多信息:https://msdn.microsoft.com/en-us/library/ms173121.aspx
I think it would be the best to reference you to here to read some more about the topic: https://msdn.microsoft.com/en-us/library/ms173121.aspx
我重新编写了您的代码以包含所有这些提示(并解决您的问题)
I've re-written your code to include all those tips (and also fix your issue)
public class Oval:Shape
{
//Constructor
public Oval(double majorAxis, double minorAxis)
{
MajorAxis=majorAxis;
MinorAxis=minorAxis;
}
protected double MajorAxis{ get; set; }
protected double MinorAxis{ get; set; }
}
public class Circle:Oval
{
//Constructor
public Circle(double radius): base(radius,radius)
{
radius = Circle_Radius;
}
public double Radius
{
get
{
return MajorAxis;
}
set
{
MajorAxis = value;
MinorAxis = value;
}
}
}
这篇关于解决错误“没有给出对应于所需形式参数的参数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!