“类型赋值3:四边形不包含带0参数的构造函数”C#

时间:2015-12-04 20:43:22

标签: c#

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



    }


}

我认为我已经在主类中定义了构造函数,我是否需要覆盖派生类的构造函数?为什么代码没有成功? 谢谢你的帮助。

3 个答案:

答案 0 :(得分:0)

Quadrilateral quad = new Quadrilateral();

您需要在声明新的四边形时传入变量。您的错误消息是说您没有接受0参数的构造函数,但是您声明了一个新的Quadrilateral对象并且没有传递任何参数。

试试这个:

Quadrilateral quad = new Quadrilateral(Q, W, E, R, T);

答案 1 :(得分:0)

由于您的Quadrilateral类构造函数包含4个参数,编译器将不再为您生成默认的0参数构造函数。

前一段时间我有同样的问题并将其用作参考。它将更详细地解释这一点:Should we always include a default constructor in the class?

答案 2 :(得分:0)

构造函数不是继承的。

Trapezoid类只有默认的无参数构造函数。这个构造函数试图调用不存在的基类的无参数构造函数,因为你实现了另一个构造函数。

您需要显式创建构造函数并调用正确的基类构造函数。

class Trapezoid : Quadrilateral
{
    public Trapezoid(int Q, int W, int E, int R, int T)
        : base(Q, W, E, R, T)
    { }
    // ...
}

顺便说一下,构造函数中的赋值是不正确的。您应该为属性分配参数,而不是其他方式。另外,尝试为变量和参数找到有意义的名称。