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);
}
}
我以爲我已經定義了主類的構造函數,我是否需要重寫派生類的構造函數?爲什麼代碼不工作? 感謝您的幫助。「類型assignment3:四邊形不包含一個構造函數,它需要0參數」C#
非常感謝你們,我終於解決了這個問題! – Yuji