進哥的布拉格

Chin Gooole's Blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

注意Destructor的 this,預設會去呼叫 this.ToString()

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);
   }

    
~Point()
    {
        Console.WriteLine(
"Destructed {0}"this);
    }
    
public override string ToString()
    {
        
return string.Format("({0}, {1})", x, y);
    }
}
class Test
{
   
static void Main() {
      Point a 
= new Point();
      Point b 
= new Point(34);
      
double d = Point.Distance(a, b);
      Console.WriteLine(
"Distance from {0} to {1} is {2}", a, b, d);
   }
}
posted on 2008-10-30 17:04  進哥  阅读(168)  评论(0编辑  收藏  举报