2015-12-04 35 views
0
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#

回答

0
Quadrilateral quad = new Quadrilateral(); 

你需要傳遞變量,同時聲明一個新的四邊形。您的錯誤消息是說您沒有接受0參數的構造函數,但是您聲明瞭一個新的Quadrilateral對象並且不傳遞參數。

試試這個:

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

非常感謝你們,我終於解決了這個問題! – Yuji

0

既然你有你的四邊形類的構造函數有4個參數,編譯器將不再生成一個默認的0參數的構造函數爲您服務。

我曾經有過這個相同的問題,並用它作爲參考。這將進一步詳細解釋:Should we always include a default constructor in the class?

+0

非常感謝你們,我終於解決了這個問題! – Yuji

0

構造函數沒有被繼承。

Trapezoid類只有默認的無參數構造函數。此構造函數試圖調用不存在的基類的無參數構造函數,因爲您實現了另一個構造函數。

您需要顯式創建構造函數並調用正確的基類構造函數。

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

順便說一句,構造函數中的賦值是不正確的。您應該將參數分配給屬性,而不是其他方式。另外,嘗試爲變量和參數找到有意義的名稱。

+0

非常感謝你們,我終於解決了這個問題!是的,你是對的!我真的應該避免我的變量和參數的無意義的名稱。有時候會讓我困惑。很多thx! ^^ – Yuji

+0

@Yuji如果答案幫助你解決了這個問題,請[接受](http://stackoverflow.com/help/accepted-answer)其中之一。它會向其他用戶顯示你的問題已經解決,不需要進一步的幫助。 –

相關問題