C# 类构造函数中存在多个嵌入的对象错误数组
有两类:点和三角形。 点本身就是点,三角形应该有一个包含3个类Point的嵌入对象的数组 “点”工作正常,但在创建三角形时出现了一系列错误。 我哪里出错了 要点:C# 类构造函数中存在多个嵌入的对象错误数组,c#,class,console,C#,Class,Console,有两类:点和三角形。 点本身就是点,三角形应该有一个包含3个类Point的嵌入对象的数组 “点”工作正常,但在创建三角形时出现了一系列错误。 我哪里出错了 要点: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Classes { class Point { private int x; private
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[]*/
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间类
{
阶级三角
{
私有点pnt[];/*错误数组声明符*/
公共三角()
{
pnt=新点[3];/*无法将点[]转换为点*/
对于(inti=0;i第一个错误-您不能这样声明数组
private Point[] pnt;
新建点[3]
创建一个包含3项的点的数组。您不能将此数组分配给点
类型的对象(此时即pnt
)
由于pnt
是一个点
对象而不是数组,因此它没有Length
属性。这也是为什么不能对其应用索引(pnt[i]
)
所有其他错误都源于第一个错误-数组声明不正确。第一个错误-您不能以这种方式声明数组
private Point[] pnt;
新建点[3]
创建一个包含3项的点的数组。您不能将此数组分配给点
类型的对象(此时即pnt
)
由于pnt
是一个点
对象而不是数组,因此它没有Length
属性。这也是为什么不能对其应用索引(pnt[i]
)
所有其他错误都源于第一个错误-数组声明不正确。在C#中是:
在C#中,它是:
不能声明如下数组类型的变量:
private Point pnt[];
private Point[] pnt;
它必须是这样的:
private Point pnt[];
private Point[] pnt;
这也是Java(我想你更熟悉的语言)中的首选方式,尽管Java也允许使用前一个版本。在我看来,这更有意义,因为它将所有类型信息保存在一个地方,与变量名分开
接下来,您可能应该了解如何在C#中指定属性—使用getX
和setX
使其看起来更像Java而不是C#。不能声明如下数组类型的变量:
private Point pnt[];
private Point[] pnt;
它必须是这样的:
private Point pnt[];
private Point[] pnt;
这也是Java(我想你更熟悉的语言)中的首选方式,尽管Java也允许使用前一个版本。在我看来,这更有意义,因为它将所有类型信息保存在一个地方,与变量名分开
接下来您可能需要了解的是如何在C#中指定属性—使用getX
和setX
使其看起来更像Java而不是C#;
公共三角()
{
pnt=新点[3];
对于(int i=0;i
另外,System.Drawing已经有了一个您可能想要使用的点结构。Point[]pnt;
公共三角()
{
pnt=新点[3];
对于(int i=0;i
另外,System.Drawing已经有一个您可能想要使用的点结构。我将尝试解释错误消息:
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=新点[3];/*无法将点[]转换为点*/
对于(int i=0;i我将尝试解释错误消息:
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=新点[3];/*无法将点[]转换为点*/
对于(inti=0;iAs,我看到它new Point[3]
创建了一个包含3个元素的点数组-如Point[]p=new Point[3];
@svrist-感谢您的澄清。非常正确。答案已更正。@Oded:进一步的更正建议:将Point[3]
更改为new Point[3]
,以明确这不是在应用索引器。@Jon-是的,我意识到了。我误读了代码(将我的期望值替换为发布的实际代码)。答案已更正。正如我所见,新点[3]
创建了一个包含3个元素的点数组-如点[]p=new点[3]
@svrist-谢谢你的澄清。非常正确。答案已更正。@Oded:进一步的更正建议:将点[3]
更改为新点[3]
,以明确这不是在应用索引器。@Jon-是的,我意识到了。我误读了代码(用我的期望值替换实际发布的代码)。答案已更正。