如何修复控制台写入 Class 的名称?
How to fix Console Writing the name of the Class?
我是 C# 的新手,几个小时以来我一直在努力解决这个问题,非常感谢您的帮助。
我想创建一个多边形并记下每个点的位置。
目前我有这个:
-Class点
class Point
{
private int x;
private int y;
public Point(int x2, int y2)
{
x = x2;
y = y2;
}
}
-Class多边形
class Polygon
{
private Point[] Points;
public Polygon(params Point[] a)
{
Points = new Point[a.Length];
for (int i = 0; i < a.Length; i++)
{
Points[i] = a[i];
}
}
public Point this[int index]
{
get { return Points[index]; }
set { Points[index] = value;}
}
}
现在我的主程序中有这个:
Polygon First= new Polygon(new Point(7,4), new Point(4,1), new Point(2, 1));
First[0] = new Point(3, 4);
Console.WriteLine("points of polygon ");
for (int i = 0; i < First.PointCounter; i++)
{
Console.WriteLine(First[i]);
}
但现在我没有在 "points of polygon" 之后看到点的每个位置,而是在我的控制台中看到了这个:https://imgur.com/Z5aVFMK
它应该是什么样子:https://imgur.com/a/aFkdrEF
它应该是什么样子:https://imgur.com/a/aFkdrEF
C# 与其他语言不同 "interpreted",因此 Console.WriteLine
方法不会猜测您要打印的内容。
要使用当前代码给出您正在寻找的结果,您必须为 Point
class:
提供 public 属性
public int X { get { return x;} set{ x = value;} }
public int Y { get { return y;} set{ y = value;} }
之后,您现在可以在 for 循环中访问这些属性:
for (int i = 0; i < First.PointCounter; i++)
{
Console.WriteLine($"x:{First[i].X} y:{First[i].Y}");
}
我添加了对 ToString
的覆盖,以便您的 Point
class 在转换为字符串时具有预期的输出。像 "x:3 y:4"
.
这样的输出
class Point
{
public int x { get; private set; }
public int y { get; private set; }
public Point(int x2, int y2)
{
x = x2;
y = y2;
}
public override string ToString()
{
return $"x:{x,-3} y:{y,-3}";
}
}
就像现在一样,它是成为 struct
而不是 class
的一个很好的候选人。
我是 C# 的新手,几个小时以来我一直在努力解决这个问题,非常感谢您的帮助。
我想创建一个多边形并记下每个点的位置。
目前我有这个: -Class点
class Point
{
private int x;
private int y;
public Point(int x2, int y2)
{
x = x2;
y = y2;
}
}
-Class多边形
class Polygon
{
private Point[] Points;
public Polygon(params Point[] a)
{
Points = new Point[a.Length];
for (int i = 0; i < a.Length; i++)
{
Points[i] = a[i];
}
}
public Point this[int index]
{
get { return Points[index]; }
set { Points[index] = value;}
}
}
现在我的主程序中有这个:
Polygon First= new Polygon(new Point(7,4), new Point(4,1), new Point(2, 1));
First[0] = new Point(3, 4);
Console.WriteLine("points of polygon ");
for (int i = 0; i < First.PointCounter; i++)
{
Console.WriteLine(First[i]);
}
但现在我没有在 "points of polygon" 之后看到点的每个位置,而是在我的控制台中看到了这个:https://imgur.com/Z5aVFMK
它应该是什么样子:https://imgur.com/a/aFkdrEF
它应该是什么样子:https://imgur.com/a/aFkdrEF
C# 与其他语言不同 "interpreted",因此 Console.WriteLine
方法不会猜测您要打印的内容。
要使用当前代码给出您正在寻找的结果,您必须为 Point
class:
public int X { get { return x;} set{ x = value;} }
public int Y { get { return y;} set{ y = value;} }
之后,您现在可以在 for 循环中访问这些属性:
for (int i = 0; i < First.PointCounter; i++)
{
Console.WriteLine($"x:{First[i].X} y:{First[i].Y}");
}
我添加了对 ToString
的覆盖,以便您的 Point
class 在转换为字符串时具有预期的输出。像 "x:3 y:4"
.
class Point
{
public int x { get; private set; }
public int y { get; private set; }
public Point(int x2, int y2)
{
x = x2;
y = y2;
}
public override string ToString()
{
return $"x:{x,-3} y:{y,-3}";
}
}
就像现在一样,它是成为 struct
而不是 class
的一个很好的候选人。