“类型赋值 3:四边形不包含采用 0 参数的构造函数” C#
本文关键字:参数 构造函数 包含采 赋值 类型 类型赋值 四边形 | 更新日期: 2023-09-27 18:25:52
public class Quadrilateral
{
public int Xb{get; set;}
public int Xc{get; set;}
public int Xd{get; set;}
public int Yc{get; set;}
public int Yd{get; set;}
public Quadrilateral(int Q, int W, int E, int R, int T)
{
Q = Xb;
W = Xc;
E = Yc;
R = Xd;
T = Yd;
}//end of the constructor
public void inspectshape(int Q,int W,int E,int R,int T)
{
if (Yc == Yd) {
Trapezoid trap = new Trapezoid ();
trap.Area (Q,W,E,R,T);
} else if (Yc == Yd && (Xc - Xd) == Xb) {
Parallelogram para = new Parallelogram ();
para.Area (Q,W,E,R,T);
} else if (Yc == Yd && (Xc - Xd) == Xb && Xd == 0) {
Rectangle rec = new Rectangle ();
rec.Area (Q,W,E,R,T);
} else if (Yc == Yd && (Xc - Xd) == Xb && Xd == 0 && Xc == Yc) {
Square sq = new Square ();
sq.Area (Q,W,E,R,T);
} else {
Quadrilateral quad = new Quadrilateral() ;
quad.Area (Q, W, E, R, T); *here is the error*
}
}//end of the method inspectshape
public virtual void Area(int Q, int W, int E, int R, int T)
{
double a = Q;//side length of AB
double b = System.Math.Sqrt ((Q - W)*(Q - W) + (0 - E)*(0-E)); //side length of BC
double c = System.Math.Sqrt ((W - R)*(W - R) + (E - T)*(E-T)); //side length of CD
double d = System.Math.Sqrt (R*R + T*T); //side length of DA
double z = (a + b + c + d) / 2; //irregular quadrilateral parameter z
double Area = System.Math.Sqrt ((z - a) * (z - b) * (z - c) * (z - d)); //area of the quadrilateral
Console.WriteLine ("Depends on the numbers given, this is a quadrilateral, and its area is" + Area);
}//end of the method area
}//end of the class quadrilateral
class Trapezoid : Quadrilateral *here is another same type error*
{
public override void Area(int Q, int W, int E, int R, int T)
{
double a = System.Math.Sqrt ((W - R)*(W - R) + (E - T)*(E-T)); //upper side length or could use Xc-Xd
double b = Q; //bottom side length
double h = E; //the height of the trapezoid
double Area = 0.5 * (a + b) * h;
Console.WriteLine ("Depends on the numbers given, this is a trapezoid, and its area is" + Area);
}//end of the method area
}//end of the class trapezoid
class Parallelogram : Trapezoid
{
public override void Area(int Q, int W, int E, int R, int T)
{
double b = Q;
double h = T;
double Area = b * h;
Console.WriteLine ("Depends on the numbers given, this is parallelogram, and its area is" + Area);
}//end of the method area
}//end of the class parallelogram
class Rectangle : Parallelogram
{
public override void Area(int Q, int W, int E, int R, int T)
{
double w = Q;
double h = E;
double Area = w * h;
Console.WriteLine ("Depends on the numbers given, this is a rectangle, and its area is" + Area);
}//end of the method area
}//end of the class rectangle
class Square : Rectangle
{
public override void Area(int Q, int W, int E, int R, int T)
{
double a = Q;
double Area = a * a;
Console.WriteLine ("Depends on the numbers given, this is a square, and its area is" + Area);
}
}//end of the class square;
public class Program
{
static void Main(string[] args) //this is the entry point of my program
{
Console.WriteLine ("Welcome to Quadrilateral shape inspection & area calculation system.");
Console.Write ("Please give a integer for the point Xb: ");
int Xb = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Please give a integer for the point Xc: ");
int Xc = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Please give a integer for the point Yc: ");
int Yc = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Please give a integer for the point Xd: ");
int Xd = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Please give a integer for the point Yd: ");
int Yd = Convert.ToInt32 (Console.ReadLine ());
Quadrilateral quad = new Quadrilateral (Xb,Xc,Yc,Xd,Yd);
quad.inspectshape (Xb,Xc,Yc,Xd,Yd);
}
}
我以为我已经在主类中定义了构造函数,我是否需要覆盖派生类中的构造函数?为什么代码不起作用?谢谢你的帮助。
Quadrilateral quad = new Quadrilateral();
您需要在声明新的四边形时传入变量。您的错误消息说您没有接受 0 参数的构造函数,但您正在声明一个新的四边形对象并且不向其传递任何参数。
试试这个:
Quadrilateral quad = new Quadrilateral(Q, W, E, R, T);
由于您的四边形类构造函数具有 4 个参数,因此编译器将不再为您生成默认的 0 参数构造函数。
不久前我遇到了同样的问题,并将其用作参考。它将更详细地解释这一点:我们是否应该始终在类中包含默认构造函数?
构造
函数不是继承的。
Trapezoid
类只有默认的无参数构造函数。此构造函数尝试调用不存在的基类的无参数构造函数,因为您实现了另一个构造函数。
您需要显式创建一个构造函数并调用正确的基类构造函数。
class Trapezoid : Quadrilateral
{
public Trapezoid(int Q, int W, int E, int R, int T)
: base(Q, W, E, R, T)
{ }
// ...
}
顺便说一下,构造函数中的赋值不正确。应将参数分配给属性,而不是其他方式。此外,请尝试为变量和参数查找有意义的名称。