日期:2014-05-17  浏览次数:20718 次

构造函数 类的问题。。。初学者 有疑问!!!

//这个是Point的类
using System;
using System.Collections.Generic;  
using System.Linq;
using System.Text;

namespace ClassDo  
{
  class Point
  {  
  private int x; 
  private int y;
 
  public Point()
  {
  x = -1;
  y = -1;
  }
  public Point(int h, int z)
  {
  x = h;
  y = z;
  }

  public double Distance(Point p)
  {
  int xdiff = x - p.x;
  int ydiff = y - p.y;
  return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
  }
  }
}

//主

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassDo
{
  class Program
  {
  static void Main(string[] args)
  {
  int x2 = int.Parse(Console.ReadLine());
  int y2 = int.Parse(Console.ReadLine());
  Point p1 = new Point();
  Point p2 = new Point(x2, y2);
  double distance=p1.Distance(p2);
  Console.WriteLine(distance);
   
  }
  }
}


上面的 public double Distance(Point p) 这里的Point p是什么意思,刚刚学,有点模糊又看不懂,求老师解答 谢谢!!

------解决方案--------------------
参数啊 Point类型的参数
------解决方案--------------------
Point p表示另一个点。

这个函数的作用是,传入另一个点,返回这个点到那个点的距离,公式是勾股定律。(x2-x1)^2+(y2-y1)^2再开根号(sqrt)
------解决方案--------------------
Point p1 = new Point(0,0);
Point p2 = new Point(5,5);
p1.Distance(p2);
这就是Distance的调用,在public double Distance(Point p)函数中p就是传入的p2. 


lz找本最基础的书看一看了。