2013-03-01 70 views
1

所以我在java中爲學校做了一個任務...這是一個類層次結構類型的任務,我們應該創建一個「Triangle.java」類,該類擴展了一個「ClosedShape.java」類,該類擴展了「Shape.java」.... ClosedShape和Shape都給予了我們,所以它們很可能沒有錯(我'反正會後他們),但我的三角類如下:構造函數...不適用(實際和正式參數列表的長度不同) - 但它確實匹配

public abstract class Triangle extends ClosedShape{ 

     public Triangle(int[] a, int[] b, int base, int height){ 
      super(true, 3, a, b); 
      setWidth(base); 
      setHeight(height); 
      setXYCoords(a, b); 
     } 

     public Triangle(int x, int y, int base, int height){ 
      int[] a = new int[3]; 
      int[] b = new int[3]; 

      a[0] = x; 
      a[1] = (x+base)/2; 
      a[2] = (x+base); 

      b[0] = y; 
      b[1] = (y+height)/2; 
      b[2] = (y+height); 
     } 

} 

我之所以有兩個構造是因爲我需要創建這兩個數組來保存點繪製形狀......然後我需要讓它們傳入ClosedShape(boolean,int,int [],int [])超類......如果我在調用super()之前,需要在同一個構造函數中創建數組,以便它們可以被傳入,但這是不允許的,因爲對super()的調用必須是第一個...所以當前,當我嘗試編譯Triangle.java我得到的錯誤:

Triangle.java.14: error: no suitable constructor found for ClosedShape() 
    { //little arrow pointing under the '{' 

constructor ClosedShape.ClosedShape(boolean, int, int[], int[]) is not applicable 
    (actual and formal argument lists differ in length)  
    constructor ClosedShape.ClosedShape(boolean, int) is not applicable 
    (actual and formal argument lists differ in length) 
1 error 

還指定了分配,對於三角形簽名必須是黃金三角(INT X,INT Y,INT基地,INT高度)...所以....我很困惑,因爲如果我沒有弄錯(哪些java相信我是...)我用所有適當的值做了一個超級調用,並且是一個構造函數「ClosedShape(boolean,int ,int [],int [])「...繼承ClosedShape類:

import java.awt.Graphics; 

public abstract class ClosedShape extends Shape { 
    boolean polygon; 
    int numPoints; 
    int[] xVertices; 
    int[] yVertices; 
    int x,y,width, height; 

    public ClosedShape(boolean isPolygon, int numPoints) { 
     super(0,0); 
     this.polygon = isPolygon; 
     this.numPoints = numPoints; 
    } 

    public ClosedShape(boolean isPolygon, int numPoints, int[] x, int[] y) { 
     super(x[0],y[0]); 
     this.polygon = isPolygon; 
     if (isPolygon) { 
      this.numPoints = numPoints; 
      xVertices = new int[numPoints]; // error check? if x.length == numPoints 
      //for (int i = 0; i < x.length; i++) { // make copy of array: why? 
      // xVertices[i] = x[i]; 
      //} 
      yVertices = new int[numPoints]; // error check? if y.length == numPoints 
      for (int i = 0; i < y.length; i++) { // make copy of array 
        yVertices[i] = y[i]; 
      } 
     } 
     else { // its an oval - define bounding box 
      this.numPoints = 4; 
      this.x = x[0]; 
      this.y = y[0]; 
      width = x[1]; 
      height = y[1]; 
     } 
    } 

    public void setXYCoords(int[] x, int[] y){ 
     this.xVertices = x; 
     this.yVertices = y; 
    } 

    // Gives access to the width attribute 
    public void setWidth(int width){ 
     this.width = width; 
    } 

    // Gives access to the height attribute 
    public void setHeight(int height) { 
     this.height = height; 
    } 

    public void draw(Graphics g) { 
     if (polygon) { 
      g.drawPolygon(xVertices, yVertices, numPoints); 
     } 
     else { 
      g.drawOval(x, y, width, height); 
     } 

    } 

    public abstract double Area(); 
    public abstract double Perimeter(); 








} 
+4

我的建議是?相信編譯器。你的代碼是錯誤的。 – duffymo 2013-03-01 03:10:30

+0

將第二個構造函數更改爲public void Triangle(...)使其不再是構造函數。它現在成爲三角形上的一種普通方法。因此,它現在編譯是因爲你改變了你有問題的構造函數而不是一個構造函數。 – Marshmellow1328 2013-03-01 03:18:52

+0

這是瘋狂的建議。如果您需要構造函數,請正確編寫它。 – duffymo 2013-03-01 03:23:53

回答

1

我終於明白了。我正在調用一個我甚至不需要使用的構造函數!我只需要對第一個構造函數的調用,然後使用setXYCoords()方法做什麼,我需要用數組做....繼承人我的最終代碼:

(ClosedShape.java保持不變)

import java.awt.Graphics; 

public class Triangle extends ClosedShape{ 

    public Triangle(int x, int y, int base, int height){ 
     super(true, 3); 

     setWidth(base); 
     setHeight(height); 

     int [] arrayX = new int[3]; 
     arrayX[0] = x; 
     arrayX[1] = (x+(width/2)); 
     arrayX[2] = (x+width); 

     int [] arrayY = new int[3]; 
     arrayY[0] = y; 
     arrayY[1] = (y-height); 
     arrayY[2] = y; 

     setXYCoords(arrayX, arrayY); 

    } 

    public double Area(){ 
       return 0; 
    } 

    public double Perimeter(){ 
     return 0; 
    } 


} 
4

問題是ClosedShape沒有默認的無參數構造函數。

看這個構造函數:

public Triangle(int x, int y, int base, int height){ 

有一個super()構造函數沒有顯式調用,因此編譯器假定它需要調用無參數的構造函數。但沒有一個...

+0

更好地檢查自己,user93353。這是另一個問題,而不是你正在看的問題。 – duffymo 2013-03-01 03:15:05

+0

我明白我在那裏做錯了什麼...我編輯它,並把我的單一改變哈哈,如果我沒有弄錯現在應該罰款...我希望哈哈哈它編譯:)謝謝! – MicroMumbler 2013-03-01 03:21:34

+0

那麼有什麼變化?爲ClosedShape寫一個沒有參數的構造函數,或者更好的是,向Triangle添加正確的超級調用? – duffymo 2013-03-01 03:24:54

-1

正如@duffymo所述,如果您沒有明確地調用super(),編譯器會將調用插入no-arg構造函數。

我認爲你要找的解決方案是工廠方法。例如,您可以創建一個靜態方法,即createTriangle(int x, int y, int base, int height)。在該方法中構建數組,調用相應的構造函數,然後返回構造的對象。

+0

他不需要使用靜態方法或工廠。他需要仔細觀察ClosedShape類。 – Marshmellow1328 2013-03-01 03:19:28

+0

我不認爲從Marshmellow1328的建議是最好的根據上面的評論。 – duffymo 2013-03-01 03:25:36

相關問題