我用C#建了一个项目,
内建了一个类:Point.cs:
namespace Classes
{
class Point
{
private int x, y;
public double DistanceTo(Point other)
{
int xDiff =this.x-other.x;
int yDiff=this.y-other.y;
return Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
}
public Point()
{
this.x=-1;
this.y=-1;
Console.WriteLine("default constructor called");
}
public Point(int x, int y)
{
Console.WriteLine("x:{0},y:{1}", x, y);
}
}
}
接着建了一个program.cs:
namespace Classes
{
class Program
{
static void Entrance()
{
Point origin = new Point();
Point bottomRight = new Point(1024, 1280);
double distance = origin.DistanceTo(bottomRight);
Console.WriteLine("Distance is : {0}", distance);
} static void Main(string[] args)
{
try
{
Entrance();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
}
打开时,得出的结果distanse却是0,我自己看几遍,发现DistanceTo中
int xDiff =this.x-other.x;
int yDiff=this.y-other.y;
发现后面的other.x和other.y有跟没有都一样呢,
我实在不明白为什么?如果因为x,y是Point类的私有字段的话,不能访问的话,它应该调试错误呀,可是可以调试运行,得到
的是2的平方差,可是没有错的话,它应该给other.x和other.y附值呀
希望大家帮我解答解答,非常谢谢!!!
内建了一个类:Point.cs:
namespace Classes
{
class Point
{
private int x, y;
public double DistanceTo(Point other)
{
int xDiff =this.x-other.x;
int yDiff=this.y-other.y;
return Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
}
public Point()
{
this.x=-1;
this.y=-1;
Console.WriteLine("default constructor called");
}
public Point(int x, int y)
{
Console.WriteLine("x:{0},y:{1}", x, y);
}
}
}
接着建了一个program.cs:
namespace Classes
{
class Program
{
static void Entrance()
{
Point origin = new Point();
Point bottomRight = new Point(1024, 1280);
double distance = origin.DistanceTo(bottomRight);
Console.WriteLine("Distance is : {0}", distance);
} static void Main(string[] args)
{
try
{
Entrance();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
}
打开时,得出的结果distanse却是0,我自己看几遍,发现DistanceTo中
int xDiff =this.x-other.x;
int yDiff=this.y-other.y;
发现后面的other.x和other.y有跟没有都一样呢,
我实在不明白为什么?如果因为x,y是Point类的私有字段的话,不能访问的话,它应该调试错误呀,可是可以调试运行,得到
的是2的平方差,可是没有错的话,它应该给other.x和other.y附值呀
希望大家帮我解答解答,非常谢谢!!!
解决方案 »
- 请教一个简单的问题,大家帮帮忙,谢谢,关于线程与UI相关的
- vs2005里a.aspx里就几句a.aspx.cs里好多代码.我想转成视图设计但不知道怎么转.
- winform gridview 多维列标题`
- 请问如下的CSPROJ文件应该用哪个版本的.net打开啊,我用的是2003,报错说该文件格式不对
- 将字符串12345678abcde转化为:1234,5678,abcd,e怎么转?
- 要<<c#高级编程>>第三版(在线)的
- ado.net与oledb比较
- 象记事本中的查找那样,虽然失去焦点,但是界面始终在上面?
- C#打印像收款收据那样的表格要什么弄哦
- 请教:关于C# 还原数据库文件的问题
- 急求FastReport问题
- richTextBox1.SelectedText.Remove(0)为什么不能删除选中的字符串?
x:1024,y:1280
Distance is : 1.4142135623731没问题
public Point(int x, int y)
{
this.x =x;
this.y =y; //-- Console.WriteLine( "x:{0},y:{1} ", x, y);
}