2011-03-26 18 views
1

有兩個類:點和三角形。 Point本身就是一個點,Triangle應該有一個由Point 3的3個嵌入對象組成的數組。在類構造函數中嵌入多個對象錯誤數組

點工作正常,但是當創建三角形出現一堆錯誤。 我哪裏錯了?

點:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Classes 
{ 
    class Point 
    { 
     private int x; 
     private int y; 

     public int getX() 
     { 
      return x; 
     } 
     public int getY() 
     { 
      return y; 
     } 

     public void setX() 
     { 
      //get user input, validate 
      Console.WriteLine("Enter X co-ord: "); 
      int inputX; 
      if (Int32.TryParse(Console.ReadLine(), out inputX)) 
      { 
       setX(inputX); 
      } 
      else 
      { 
       Console.WriteLine("Invalid input value"); 
       Console.WriteLine("Assigning default value of 0 to X co-ord"); 
       setX(0); 
      } 
     } 
     public void setX(int xx) 
     { 
      x = xx; 
     } 
     public void setX(Point px) 
     { 
      if (px == null) 
       x = 0; 
      else 
       x = px.x; 
     } 
     public void setY() 
     { 
      //get user input, validate 
      Console.WriteLine("Enter Y co-ord: "); 
      int inputY; 
      if (Int32.TryParse(Console.ReadLine(), out inputY)) 
      { 
       setY(inputY); 
      } 
      else 
      { 
       Console.WriteLine("Invalid input value"); 
       Console.WriteLine("Assigning default value of 0 to Y co-ord"); 
       setY(0); 
      } 
     } 
     public void setY(int yy) 
     { 
      y = yy; 
     } 
     public void setY(Point py) 
     { 
      if (py == null) 
       y = 0; 
      else 
       y = py.y; 
     } 

     public void setPoint() 
     { 
      setX(); 
      setY(); 
     } 
     public void setPoint(int xx, int yy) 
     { 
      setX(xx); 
      setY(yy); 
     } 

     public void setPoint(Point p) 
     { 
      setX(p); 
      setY(p); 
     } 

     public Point() 
     { 
      setPoint(); 
     } 

     public Point(int xx, int yy) 
     { 
      setPoint(xx, yy); 
     } 

     public Point(Point p) 
     { 
      setPoint(p); 
     } 

     public static Point operator +(Point p1, Point p2) 
     { 
      Point temp = new Point(0,0); 
      temp.x = p1.getX() + p2.getX(); 
      temp.y = p1.getY() + p2.getY(); 
      return temp; 
     } 

     public static Point operator -(Point p1, Point p2) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = p1.getX() - p2.getX(); 
      temp.y = p1.getY() - p2.getY(); 
      return temp; 
     } 

     public static Point Add(Point p1, Point p2) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = p1.getX() + p2.getX(); 
      temp.y = p1.getY() + p2.getY(); 
      return temp; 
     } 

     public static Point Add(int xx, int yy ,Point p) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = xx + p.getX(); 
      temp.y = yy + p.getY(); 
      return temp; 
     } 

     public static Point Add(int x1, int x2, int y1, int y2) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = x1 + x2; 
      temp.y = y1 + y2; 
      return temp; 
     } 

     public Point Add(Point p) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = getX() + p.x; 
      temp.y = getY() + p.y; 
      return temp; 
     } 

     public Point Add(int xx, int yy) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = getX() + xx; 
      temp.y = getY() + yy; 
      return temp; 
     } 

     public static Point Subtract(Point p1, Point p2) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = p1.getX() - p2.getX(); 
      temp.y = p1.getY() - p2.getY(); 
      return temp; 
     } 

     public static Point Subtract(int xx, int yy, Point p) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = xx - p.getX(); 
      temp.y = yy - p.getY(); 
      return temp; 
     } 

     public static Point Subtract(int x1, int x2, int y1, int y2) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = x1 - x2; 
      temp.y = y1 - y2; 
      return temp; 
     } 

     public Point Subtract(Point p) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = getX() - p.x; 
      temp.y = getY() - p.y; 
      return temp; 
     } 

     public Point Subtract(int xx, int yy) 
     { 
      Point temp = new Point(0, 0); 
      temp.x = getX() - xx; 
      temp.y = getY() - yy; 
      return temp; 
     } 

     public void displayPoint() 
     { 
      System.Console.WriteLine("Point: X={0} ; Y={1}", getX(), getY()); 
     } 

     public bool isEqual(Point p) 
     { 
      if (x == p.x && y == p.y) 
       return true; 
      else 
       return false; 
     } 

     public static bool isEqual(Point p1, Point p2) 
     { 
      if (p1.x == p2.x && p1.y == p2.y) 
       return true; 
      else 
       return false; 
     } 

     public bool isNotEqual(Point p) 
     { 
      if (this.isEqual(p) == true) 
       return false; 
      else 
       return true; 
     } 

     public static bool isNotEqual(Point p1, Point p2) 
     { 
      if (Point.isEqual(p1, p2) == true) 
       return true; 
      else 
       return false; 
     } 

     public static bool operator ==(Point p1, Point p2) 
     { 
      if (p1.isEqual(p2) == true) 
       return true; 
      else 
       return false; 
     } 

     public static bool operator !=(Point p1, Point p2) 
     { 
      if (p1.x == p2.x && p1.y == p2.y) 
       return true; 
      else 
       return false; 
     } 
    } 

} 

三角:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Classes 
{ 
    class Triangle 
    { 
     private Point pnt[]; /*Bad array declarator*/ 

     public Triangle() 
     { 
      pnt = new Point[3]; /*Can't convert Point[] to Point*/ 
      for (int i=0; i<pnt.Length; i++) /*Length doesn't exists in Point*/ 
       pnt[i] = new Point(); /*Can't apply indexing to Point[]*/ 
     } 
    } 
} 

回答

0

第一個錯誤 - 你不聲明數組的方式。

private Point[] pnt; 

new Point[3]創建的Point與3項的數組。您不能將此數組分配給Point類型(在這一點上是什麼pnt是一個對象。

由於pntPoint對象一個數組,它不會有一個Length財產。這也是爲什麼你可以不適用索引(pnt[i])將其


所有其他錯誤,從第一個跟進 - 。不正確的數組聲明

+0

@Downvoter - 謹慎評論? – Oded 2011-03-26 19:31:53

+1

正如我所看到的'新的Point [3]'創建一個包含3個元素的Point數組 - 'Point [] p = new Point [3];' – svrist 2011-03-26 19:32:23

+0

@svrist - 謝謝澄清。完全正確。答案已更正。 – Oded 2011-03-26 19:34:26

4

在C#,它是:

private Point[] pnt; 
2

不能聲明一個數組類型的這樣的變量:

private Point pnt[]; 

它必須是這樣的:

private Point[] pnt; 

這就是 Java中的首選方式(這是我認爲你更熟悉的語言),儘管Java也允許你使用前一版本。我認爲更有意義,因爲它將所有類型信息保存在一個地方,與變量名分開。

您應該看看的下一件事是如何在C#中指定屬性 - 使用getXsetX使它看起來更像Java而不是C#。

0
Point[] pnt; 
public Triangle() 
{ 
    pnt = new Point[3]; 
    for (int i = 0; i < pnt.Length; ++i) pnt[i] = new Point(); 
} 

另外,System.Drawing已經有了一個您可能想要使用的Point結構。

0

我會盡力解釋錯誤信息:

private Point pnt[]; /*Bad array declarator*/ 

數組聲明這是「[]」必須站在正後方的類型,而不是背後的變量,因此將該行更改爲private Point[] pnt;

pnt = new Point[3]; /*Can't convert Point[] to Point*/ 
for (int i=0; i<pnt.Length; i++) /*Length doesn't exists in Point*/ 
    pnt[i] = new Point(); /*Can't apply indexing to Point[]*/ 

糾正數組聲明後,以下消息將消失。在上面的代碼中,pnt的類型仍然是Point,因此訪問Array方法或索引將失敗。