我需要创建类Circle,其中心定义为Point类型,但我没有一个概念如何设置中心值,显示它们(在控制台中写入每个圆圈数据),接下来我喜欢它们。
我有这样的事情:
namespace Circle
{
class Program
{
class Point
{
private float x, y;
public Point()
{
x = 3.14f;
y = 3.14f;
}
public Point(float a, float b)
{
x = a;
y = b;
}
public float X
{
get { return x; }
set { x = value; }
}
public float Y
{
get { return y; }
set { y = value; }
}
public void Show()
{
Console.WriteLine("X = {0}, Y = {1}", x, y);
}
}
class Circle
{
private Point center;
private float radius;
public Circle(Point s = null, float r = 1)
{
if (s == null) center = new Point(0,0);
radius = r;
}
public float Ra
{
get { return radius; }
set { radius = value; }
}
public Point Ce
{
get { return center; }
set { center = value; }
}
}
static void Main(string[] args)
{
Point p;
p = new Point();
p.X = 1;
p.Y = 1;
Circle k;
k = new Circle();
k.Ce = p;
k.Ra = 19;
Console.WriteLine("center = {0}, radius = {1}", k.Ce, k.Ra);
Console.ReadKey();
}
}
}
现在程序编译,但显示奇怪的东西(中心= Circle.Program +点)。
PS。我需要将private float x, y;
保持在Point
类中作为private
。
您尝试输出的对象属性是:
k.Ce
这个属性的类型是Point,因为.ToString()还没有实现,输出会很奇怪,尝试将ToString实现到你的Point类中,如下所示:
public override string ToString() {
return "X =" + x + ", Y = " + y;
}
结果现在可读性很强。