C#知识,请大哥们帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/05/05 08:47:04
using System;
class Point
{
public double x,y;
public Point()
{
this.x=0;
this.y =0;
}

public Point(double x,double y )
{
this.x =x;
this.y=y;
}
public static double Distance (Point a,Point b )
{
double xdiff=a.x -b.x ;
double ydiff=a.y -b.y ;
return Math.Sqrt (xdiff*xdiff+ydiff*ydiff);
}
public override string ToString()
{
return string .Format ("({0},{1})",x,y);
}
}
class Test
{
static void Main()
{
Point a=new Point ();
Point b=new Point (3,4);
double d=Point .Distance (a,b);
Console.WriteLine("Distance from {0} to {1} is {2}",a,b,d);
}
}
//这里的 public Point()
{
this.x=0;//后面又定义public Point(double x,double y )是什么意思

this.y =0;//
}

public Point(double x,double y )
{
this.x =x;
this.y=y;

这个是多态中的重载多态,如果一个函数同名但参数表不一样,就认为是一个重载。其中Point函数本身也是构造函数。
也就是说你可以对这个类执行new Point(),也可以执行new Point(3,4)来构造它。