C#语言学习--基础部分(八) --类->构造方法续,静态类,静态方法

Pointer Console Demo:

 

namespace PointerDemo
{
    class Pointer
    {
        private int x;
        private int y;
        public static int count; //类的静态变量
        public Pointer()
        {
            x = -1;
            y = -1;
            count++;
        }
        public Pointer(int x, int y)
        {
            this.x = x;
            this.y = y;
            count++;
        }
        public double DistanceTo(Pointer 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 PointerDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = int.Parse(Console.ReadLine());
            int y = int.Parse(Console.ReadLine());
            Pointer p1 = new Pointer();
            Pointer p2 = new Pointer(x, y);
            double distance = p1.DistanceTo(p2);
            Console.WriteLine(distance);
            Console.WriteLine(Pointer.count);
        }
    }
}

 

posted on 2012-08-20 21:12  松波  阅读(120)  评论(0编辑  收藏  举报

导航