重载实例源代码

来源:互联网 发布:微信商城分销源码下载 编辑:程序博客网 时间:2024/06/11 06:55

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

namespace CPLR
{
    class Program
    {
        static void Main(string[] args)
        {
            CPoint a = new CPoint(4, 5);
            a--;
           CPoint a1=new CPoint (1,2);
            Console.WriteLine("点的坐标:({0},{1})",a.x ,a.y);
            CLine b= new CLine(new CPoint(1,1),new CPoint (1,2));
            CLine obj2 = new CLine(new CPoint (1,1),new CPoint (3,1));
            CPoint p=a+a1;
            Console.WriteLine("新的点:{0},{1}",p.x ,p.y);
            p = a1 % 3;
            Console.WriteLine("P点坐标:{0},{1}", p.x, p.y);
            Console.WriteLine("l1的边长:{0}",b.Leagth ());
            Console.WriteLine("l2的边长:{0}",obj2.Leagth ());
            CRect A=new CRect (b ,obj2 );
            Console.WriteLine("周长:{0}",A.C());
            Console.WriteLine("面积:{0}",A.S());
            Console.Read();
        }
    }
     class CPoint
    {
        public int x;
        public int y;
        public CPoint(int X, int Y)
        {
            x = X;
            y = Y;
        }
        public static CPoint operator --(CPoint a)
        {
            a.x = a.x - 1;
            a.y = a.y - 1;
        return a;
        }
        public static CPoint operator +(CPoint a,CPoint a1)
        {
             CPoint p = new CPoint ( a.x + a1.x,a.y + a1.y);
           
            return p;
        }
        public static CPoint operator %(CPoint b,int n)
        {
        CPoint p=new CPoint (Convert.ToInt32(Math.Pow(b.x,n)),Convert.ToInt32(Math.Pow(b.y,n)));
        return p;
        }
    }
    class CLine
    {
        public CPoint p1;
        public CPoint p2;
        public CLine (CPoint P1, CPoint P2)
        {
            p1 = P1;
            p2 = P2;
        }
        public double Leagth()
        {
            return Math.Sqrt(Math.Pow(p1.x - p2.x,2) + Math.Pow(p1.y - p2.y,2));
        }
        public static  CLine operator ++(CLine b)
        {
            b.p1.x  =b.p1.x + b.p2.x ;
            b.p1.y = b.p1.y + b.p2.y;
            return b;
        }
    }
     class CRect
    {
        public CLine l1;
        public CLine l2;
        public CRect(CLine L1, CLine L2)
        {
            l1 = L1;
            l2 = L2;
        }
        public double C()
        {
            return (l1.Leagth () +l2.Leagth()) * 2;
        }
        public double S()
        {
            return l1.Leagth() *l2.Leagth()  ;
        }
    }
}
希望大家看完贴多说说意见!大家共同学习!

谢谢!

原创粉丝点击